【问题标题】:Append file image to FormData in Javascript (Http Request)将文件图像附加到 Javascript 中的 FormData(Http 请求)
【发布时间】:2016-09-26 11:36:29
【问题描述】:

我在从 Ionic/Cordova 应用程序、javascript 将图像发布到 Web api 时遇到问题。 我参考下面的链接 link

这是我的代码:

$http({
                        method: "POST",
                        url: url,
                          headers: {
                          //'Content-Type': 'multipart/form-data',
                          //'Content-Type': false,
                          //'Content-Type': undefined,
                          'Cache-Control': 'no-cache',
                          'DevicePassword': localStorage.getItem("UserPassword"),
                          'UserName': 'thuong'
                          },
                          transformRequest: function (data) {
                          var formData = new FormData();
                          formData.append("data", angular.toJson(data.data));
                          alert("sizeofImg: " + data.files.length);

                          for (var i = 0; i < data.files.length; i++) {
                                alert(data.files[i].ImgSrc);
                                var blob = dataURItoBlob(data.files[i].ImgSrc);
                                //var file = new File([blob], "file.png",{type: 'image/png');
                                formData.append("file" + i, blob, "filename" + i + "png");
                          }
                          return formData;
                          },
                          data: { data: jsonData, files: files }
                          })
             .then(function (result) {
                   alert("success" + result);
                   success(result);
            },
                   function (error) {
                   alert("error" + JSON.stringify(error));
                   failure(error);
                   }
            );

function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
    byteString = atob(dataURI.split(',')[1]);
else
    byteString = unescape(dataURI.split(',')[1]);

// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
}

return new Blob([ia], {type:mimeString});

}

在那里,data.files[i].ImgSrc 是 base64 字符串。 (我使用 Cordova Camera 插件来获取它) 它的开头是:data:image/png;base64, ... 它总是陷入失败回调,状态码:415(UnsupportMediaFile)。我实现了服务器端,如上面的链接。 dataURIToBlob 方法对吗? 请帮我。

【问题讨论】:

    标签: javascript angularjs base64


    【解决方案1】:

    附加上传文件见代码吹

    $scope.updateProfile = function(form_data){
            console.log(form_data);
            $scope.submitted = true;
            $scope.reg = form_data;
             var fd = new FormData();
             var file = $scope.fileToUpload;
             fd.append('file',file);
             $http.post(API_URL+"user_update_profile", fd, {
                 transformRequest: angular.identity,
                 headers: {'Content-Type': undefined}
                })
                .success(function(res){
                     console.log(res);
                })
                .error(function(err){
                     console.log(err);
                });
        }
    

    注意:fileToUpload 变量是文件属性

       var file = $scope.fileToUpload;
         fd.append('file',file);
    

    【讨论】:

    • 我只有base64字符串,我不使用组件。 $scope.fileToUpload 在哪里?
    • 使用文件属性码 上面的代码应用这个文件属性,没有base64字符串这可以工作。
    • 对不起。我不使用输入标签,因为我的任务是从相机插件获取图像。所以我只有base 64。我将它转换为BLOB,现在我不知道如何发布它。
    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2019-03-18
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2017-12-09
    • 2021-05-21
    相关资源
    最近更新 更多