繁体   English   中英

在Javascript(Http请求)中将文件图像附加到FormData

[英]Append file image to FormData in Javascript (Http Request)

我正面临着从Ionic / Cordova App,javascript将图像发布到web api的问题。 我参考链接下面的链接

这是我的代码:

$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 String。 (我使用了Cordova Camera插件来获取它)它的起点如下:data:image / png; base64,...它始终属于失败回调,状态代码为:415(UnsupportMediaFile)。我实现了服务器端,如上面的链接。 dataURIToBlob方法对吗? 请帮我。

追加上传文件见代码打​​击

$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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM