当前位置: 首页 > 知识库问答 >
问题:

取消文件打开对话框时出错

赵渊
2023-03-14

当我按下文件打开对话框上的取消按钮时(代码如下),我收到以下错误:

A JavaScript error occurred in the main process

Uncaught Exception:
TypeError: Error processing argument -1.
    at TypeError (native)
    at [object Object].Object.defineProperty.set (/Applications/Electron.app/Contents/Resources/atom.asar/browser/api/lib/web-contents.js:92:24)
    at main.js:37:20
    at Function.wrappedCallback (/Applications/Electron.app/Contents/Resources/atom.asar/browser/api/lib/dialog.js:80:16)

错误显示为模式弹出窗口:

到目前为止,我只在Mac电脑上测试过。

我的代码如下:

处理创建对话框的主要过程代码如下:

import electron, { ipcMain } from 'electron'

const app = electron.app
const BrowserWindow = electron.BrowserWindow

electron.crashReporter.start()

let mainWindow = null

app.on('window-all-closed', () => {
  if (process.platform != 'darwin') app.quit()
})

app.on('ready', () => {

  mainWindow = new BrowserWindow({
    width: 800,
    height: 600
  })

  mainWindow.loadURL(`file://${__dirname}/index.html`)
  mainWindow.webContents.openDevTools()

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  ipcMain.on('file:open', (e, arg) => {
    electron.dialog.showOpenDialog(mainWindow, {
      properties: ['openFile'],
      filters: [{
        name: 'Outlines',
        extensions: ['pxo']
      }]
    }, (filePathList) => {
      debugger
      e.returnValue = filePathList
    })
  })

})

我的调试器语句从未到达。此外,当实际选择了一些文件时,代码工作正常,只有在按下“取消”时才会失败。

触发IPC调用的代码(ES6 JSX)如下所示:

class Welcome extends React.Component {

  render() {
    const styles = this.getStyles()

    return (
      <div style={styles.outerContainer}>
              <a style={styles.btnSetLink} onClick={this.initFileSelection.bind(this)}>
                Open File
              </a>
      </div>
    )
  }

  initFileSelection() {
    const filePathList = ipcRenderer.sendSync('file:open')
  }

  ...

代码通过巴别塔6进行处理。我的.babelrc如下:

{
  "presets": ["react", "es2015"]
}

电子版本:v0.35.1.

babel依赖项的版本:

├─┬ babel-preset-es2015@6.3.13
│ ├── babel-plugin-check-es2015-constants@6.3.13
│ ├── babel-plugin-transform-es2015-arrow-functions@6.3.13
│ ├── babel-plugin-transform-es2015-block-scoped-functions@6.3.13
│ ├─┬ babel-plugin-transform-es2015-block-scoping@6.3.13
│ │ ├─┬ babel-template@6.3.13
│ │ │ └── babylon@6.3.18
│ │ ├─┬ babel-traverse@6.3.19
│ │ │ ├── babylon@6.3.18
│ │ │ ├── globals@8.15.0
│ │ │ └─┬ repeating@1.1.3
│ │ │   └─┬ is-finite@1.0.1
│ │ │     └── number-is-nan@1.0.0
│ │ └─┬ babel-types@6.3.18
│ │   ├── esutils@2.0.2
│ │   └── to-fast-properties@1.0.1
│ ├─┬ babel-plugin-transform-es2015-classes@6.3.15
│ │ ├── babel-helper-define-map@6.3.13
│ │ ├── babel-helper-function-name@6.3.15
│ │ ├── babel-helper-optimise-call-expression@6.3.13
│ │ ├── babel-helper-replace-supers@6.3.13
│ │ └── babel-messages@6.3.18
│ ├── babel-plugin-transform-es2015-computed-properties@6.3.13
│ ├── babel-plugin-transform-es2015-destructuring@6.3.15
│ ├── babel-plugin-transform-es2015-for-of@6.3.13
│ ├── babel-plugin-transform-es2015-function-name@6.3.19
│ ├── babel-plugin-transform-es2015-literals@6.3.13
│ ├─┬ babel-plugin-transform-es2015-modules-commonjs@6.3.16
│ │ └── babel-plugin-transform-strict-mode@6.3.13
│ ├── babel-plugin-transform-es2015-object-super@6.3.13
│ ├─┬ babel-plugin-transform-es2015-parameters@6.3.18
│ │ ├─┬ babel-helper-call-delegate@6.3.13
│ │ │ └── babel-helper-hoist-variables@6.3.13
│ │ └── babel-helper-get-function-arity@6.3.13
│ ├── babel-plugin-transform-es2015-shorthand-properties@6.3.13
│ ├── babel-plugin-transform-es2015-spread@6.3.14
│ ├─┬ babel-plugin-transform-es2015-sticky-regex@6.3.13
│ │ └── babel-helper-regex@6.3.13
│ ├── babel-plugin-transform-es2015-template-literals@6.3.13
│ ├── babel-plugin-transform-es2015-typeof-symbol@6.3.13
│ ├─┬ babel-plugin-transform-es2015-unicode-regex@6.3.13
│ │ └─┬ regexpu@1.3.0
│ │   ├── esprima@2.7.1
│ │   ├─┬ recast@0.10.33
│ │   │ ├── ast-types@0.8.12
│ │   │ └── esprima-fb@15001.1001.0-dev-harmony-fb
│ │   ├── regenerate@1.2.1
│ │   ├── regjsgen@0.2.0
│ │   └─┬ regjsparser@0.1.5
│ │     └── jsesc@0.5.0
│ └─┬ babel-plugin-transform-regenerator@6.3.18
│   ├── babel-core@6.3.17
│   ├── babel-plugin-syntax-async-functions@6.3.13
│   ├── babylon@6.3.18
│   └── private@0.1.6
├─┬ babel-preset-react@6.3.13
│ ├── babel-plugin-syntax-flow@6.3.13
│ ├── babel-plugin-syntax-jsx@6.3.13
│ ├── babel-plugin-transform-flow-strip-types@6.3.15
│ ├── babel-plugin-transform-react-display-name@6.3.13
│ ├─┬ babel-plugin-transform-react-jsx@6.3.13
│ │ └── babel-helper-builder-react-jsx@6.3.13
│ └── babel-plugin-transform-react-jsx-source@6.3.13
├─┬ babel-register@6.3.13
│ ├─┬ babel-core@6.3.17
│ │ ├─┬ babel-code-frame@6.3.13
│ │ │ ├─┬ chalk@1.1.1
│ │ │ │ ├── ansi-styles@2.1.0
│ │ │ │ ├── escape-string-regexp@1.0.3
│ │ │ │ ├─┬ has-ansi@2.0.0
│ │ │ │ │ └── ansi-regex@2.0.0
│ │ │ │ ├── strip-ansi@3.0.0
│ │ │ │ └── supports-color@2.0.0
│ │ │ ├── js-tokens@1.0.1
│ │ │ └─┬ line-numbers@0.2.0
│ │ │   └── left-pad@0.0.3
│ │ ├─┬ babel-generator@6.3.19
│ │ │ ├─┬ detect-indent@3.0.1
│ │ │ │ ├── get-stdin@4.0.1
│ │ │ │ └── minimist@1.2.0
│ │ │ ├── is-integer@1.0.6
│ │ │ └── trim-right@1.0.1
│ │ ├── babel-helpers@6.3.13
│ │ ├── babylon@6.3.18
│ │ ├── convert-source-map@1.1.2
│ │ ├─┬ debug@2.2.0
│ │ │ └── ms@0.7.1
│ │ ├── json5@0.4.0
│ │ ├─┬ minimatch@2.0.10
│ │ │ └─┬ brace-expansion@1.1.2
│ │ │   ├── balanced-match@0.3.0
│ │ │   └── concat-map@0.0.1
│ │ ├── path-is-absolute@1.0.0
│ │ ├── shebang-regex@1.0.0
│ │ ├── slash@1.0.0
│ │ └── source-map@0.5.3
│ ├── babel-runtime@5.8.34
│ ├── core-js@1.2.6
│ ├─┬ home-or-tmp@1.0.0
│ │ ├── os-tmpdir@1.0.1
│ │ └── user-home@1.1.1
│ ├── lodash@3.10.1
│ ├── path-exists@1.0.0
│ └─┬ source-map-support@0.2.10
│   └─┬ source-map@0.1.32
│     └── amdefine@1.0.0

任何解决错误的帮助都将不胜感激。

共有3个答案

陈霄
2023-03-14

对于我的情况,我没有任何javascript错误的UI和控制台。该应用程序将只是退出,就像你刚刚关闭它正常后取消文件打开对话框。这发生在我使用电子版本5的时候。更新到版本8解决了这个问题。

张茂勋
2023-03-14

我用这个来处理

process.on('uncaughtException', function (exception) {
   // handle or ignore error
});
郁景龙
2023-03-14

同样的问题,我解决了它通过强制返回值为false(它是未定义的,否则):

ipc.on 'request:save:dialog', (event, params) ->
  params||={}
  event.returnValue = dialog.showSaveDialog(params) || false
 类似资料:
  • 问题内容: 以下代码的输出是: 以下代码显示了我的计算机上的异常。 当我运行程序时,主窗口可以正常打开,并且程序可以正常运行。 但是,如果您: 打开了使用“打开文件”按钮 按取消,然后 退出程序 一个被抛出。或者,如果您选择一个文件并“打开”,然后退出程序,则会引发相同的错误。在这个博客上,用示例代码解释了同样的事情,他的解决方案是尽快调用,而我这样做没有任何效果。 这是1.6.0_26中的错误吗

  • 问题内容: 我需要一种解决方案,以在单击时以HTML显示打开文件对话框。单击时,必须打开打开文件对话框。 我不想将输入文件框显示为HTML页面的一部分。它必须显示在单独的对话框中,该对话框不是网页的一部分。 问题答案: 这是一个不错的 它本身就是一个控件。但是div放在上面,并应用CSS样式来获得那种感觉。文件控件的不透明度设置为0,以便在单击div时似乎打开了对话框窗口。

  • 我是Java的初学者。我正在使用netbeans 7.3 IDE的GUI编辑器制作一个简单的文本编辑器。我面临的主要问题是无法保存/打开文件。我已经创建了“保存”按钮。当我删除文件选择器时,它是一个嵌入在java窗口中的正常打开的文件对话框,没有任何功能。我还尝试在单击save按钮(在源代码视图中)时创建一个新的jFileChooser,但它不起作用 简而言之,我需要一个简单的打开/保存对话框。当

  • 我试图将openFiles()方法的内脏移到actionPerformed()方法中的相应位置,但仍然没有起作用。但是,在另一个人用于不同目的的代码中,showOpenDialog()方法在从actionPerformed方法内部调用的方法中工作。代码如下: 谢谢你的帮助。

  • 我使用iText7生成PDF,然后在新选项卡中打开它。 用户的意图始终是打印,因此显示PDF并让他单击打印按钮并不方便。这就是为什么我想在PDF打开时立即打开打印对话框。 这个解决方案不起作用 使用iText打开PDF时自动打开打印对话框 因为我是派系。iText7中不存在PRINTDIALOG。 我还尝试了各种不起作用的JavaScript选项,比如 如何在页面加载后直接打开打印对话框?

  • 问题内容: 我想知道是否有跨平台的方法可以从Java Swing应用程序内部模拟Windows“打开方式”对话框。我的应用程序是用于学习软件包的编辑器,并且其中一个用户希望能够在应用程序中从他们选择的编辑器中打开内容文件,资源通常是HTML文件,图像,CSS,JavaScript,但可以是任何类型可以在浏览器中运行的内容。谢谢 问题答案: 我认为您可以使用JDIC(Java桌面集成组件)来做一些事