關於electron的跨域問題,有本地的圖片的地址,訪問不了本地的圖片


項目中有上傳圖片功能,自定義input type=file 改變透明度和超出部分隱藏,把按鈕和

點擊的圖標放在上傳文件的按鈕上面,然后又碰到到獲取input里面的圖片的本地的路徑,

在electron就是不能跨域訪問本地,如何解決呢:

百事不得其解,問了我們項目的老大,說是electron的跨域問題,改變如下

webSecurity  webPreferences:{nodeIntegration: false,  preload: js文件絕對路徑 }

 

  //發送圖片
        sendImgFile(){
            console.log("this is sendImgFile click")
            var files = this.$refs.inputImg.files; //獲取input里面的圖片的地址后者文件的本地路徑;
            var size = files[0].size;//大小
            var file_path = files[0].path;//文件的本地路徑
            this.file_paths = file_path 
            console.log("獲取圖片的files:",files,"獲取圖片的大小:",size,"獲取圖片本地的地址:",file_path);
            var content = '<img src= "' + file_path + '" class="file-img">'; 拼接img標簽
            // var content = '<img :src="'+this.file_paths+'">'
            this.$refs.chatSendContainer.innerHTML = content;
        },

 

import { BrowserWindow, globalShortcut, Menu } from 'electron'
import Common from '../common/common.js'
const winURL = process.env.NODE_ENV === 'development'
    ? `http://localhost:${require('../../../../config').port}`
    : `file://${__dirname}/index.html`

function createWindow() {
    var mainWindow = new BrowserWindow({
        height: Common.WINDOW_SIZE_LOGIN.height,
        width: Common.WINDOW_SIZE_LOGIN.width,
        resizable: false,
        frame: false,
      webPreferences:{webSecurity: false}//加上這個就可以獲取到了本地的圖片
    });
    mainWindow.loadURL(winURL);
    mainWindow.on('closed', () => {
        mainWindow = null
    });
    //前期為了調試方面,默認打開控制台
    mainWindow.webContents.openDevTools({ detach: true });
    //注冊打開控制台的快捷鍵
    globalShortcut.register('ctrl+shift+alt+e', function () {
        let win = BrowserWindow.getFocusedWindow();
        if (win) {
            win.webContents.openDevTools({ detach: true });
        }
    });


    //去掉默認菜單欄
    Menu.setApplicationMenu(null);
    // eslint-disable-next-line no-console
    console.log('mainWindow opened');
    BrowserWindow.mainWindow = mainWindow;
    return mainWindow;
}

module.exports = {
    createWindow
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM