Append image file to form data - Cordova/Angular

前端 未结 3 1279
遥遥无期
遥遥无期 2020-12-12 19:37

I am using Anuglar, Ionic and Cordova in my current project, and I\'m trying to POST FormData containing an image file to my server. Right now I\'m using the cordova camera

3条回答
  •  庸人自扰
    2020-12-12 20:26

    You do need to send file content. With the HTML5 FileAPI you need to create a FileReader object.

    Some time ago, I developed an application with cordova and I had to read some files, and I made a library called CoFS (first, by Cordova FileSystem, but it's working in some browsers).

    It's on beta state, but I use it and works well. You can try to do some like this:

    var errHandler = function (err) {
       console.log("Error getting picture.", err);
    };
    
    var sendPicture = function (file) {
    
        var fs = new CoFS();
    
        fs.readFile(file, function (err, data) {
    
            if (err) {
                return errHandler(err);
            }
    
            var fd = new FormData();
    
            fd.append('attachment', new Blob(data));
            fd.append('uuid', uuid);
            fd.append('userRoleId', userRole);
    
            console.log("Data of file:" + data.toString('base64'));
            // Send fd...
    
        });
    
    };
    
    navigator.camera.getPicture(sendPicture, errHandler);
    

    Sorry my poor english.

提交回复
热议问题