添加依赖
1 | npm install request cheerio async -save |
request
1 | var request = require('request'); |
请求跨域的解决方式
1 | // Create the browser window. |
主要添加了第6行和第11行,正式运行时删除即可
文件夹路径
主进程
1 | const { app } = require('electron') |
渲染进程
1 | const { app } = window.require("electron").remote; |
String - You can request the following paths by the name
:
home
用户的 home 文件夹(主目录)appData
Per-user application data directory, which by default points to:
%APPDATA%
Windows 中$XDG_CONFIG_HOME
or~/.config
Linux 中~/Library/Application Support
macOS 中
userData
储存你应用程序设置文件的文件夹,默认是appData
文件夹附加应用的名称temp
临时文件夹exe
当前的可执行文件module
Thelibchromiumcontent
库desktop
当前用户的桌面文件夹documents
用户文档目录的路径downloads
用户下载目录的路径music
用户音乐目录的路径pictures
用户图片目录的路径videos
用户视频目录的路径recent
Directory for the user’s recent files (Windows only).logs
应用程序的日志文件夹pepperFlashSystemPlugin
Pepper Flash 插件的系统版本的完成路径。crashDumps
Directory where crash dumps are stored.
Returns String
- A path to a special directory or file associated with name
. On failure, an Error
is thrown.
If app.getPath('logs')
is called without called app.setAppLogsPath()
being called first, a default log directory will be created equivalent to calling app.setAppLogsPath()
without a path
parameter.
选择文件夹
添加引用
1 | const { app, dialog } = window.require("electron").remote; |
弹窗选择
1 | select_dir: function() { |
文件下载
引用
1 | const request = require("request"); |
下载
1 | var fileurl = "http://www.psvmc.cn/favicon.ico"; |
文件上传
electron写应用时,会遇到自动上传的需求。但是H5中只能通过input(type=file)来手动上传,JS又没有读取文件的权限,此时,我们可以借助node模块完成需求。
1 | uploadfile: function() { |
调用接口
1 | var request = require('request'); |
按行读取文本文件
添加引用
1 | const fs = window.require("fs"); |
方法
1 | { |