angularjs - Converting base64 to file and posting using formdata append gives 0 contentlenght for httppostedfilebase -
when try convert base64 image url--> blob--> file --> post controller using formdata.append, works smooth desktop fails work in iphone 6s chrome/firefox or ie, httppostedfilebase contentlength seems "0" javascript:
croppedpicupload: function () { function datauritoblob(datauri) { // convert base64/urlencoded data component raw binary data held in string var bytestring; if (datauri.split(',')[0].indexof('base64') >= 0) bytestring = atob(datauri.split(',')[1]); else bytestring = unescape(datauri.split(',')[1]); // separate out mime component var mimestring = datauri.split(',')[0].split(':')[1].split(';')[0]; var content = new array(); (var = 0; < bytestring.length; i++) { content[i] = bytestring.charcodeat(i) } return new blob([new uint8array(content)], { type: mimestring }); } var imagebase64 = $("#croppedpic").attr("src"); var blob = datauritoblob(imagebase64); var file = new file([blob], $scope.uploadedpicturefile.name, { type: $scope.uploadedpicturefile.type }); $scope.uploadprofilepic(file, ""); }, uploadprofilepic: function (files, iconid) { var formdata = new formdata(); formdata.append("clientid", $("#partnerid").val()); formdata.append("siteid", $("#siteid").val()); formdata.append("sessionid", $("#cookievalue").val()); formdata.append("iconid", iconid); if (files != "") { alert(files); formdata.append("file", files); } $http.post("/tgnewui/profile/home/uploadprofilepic", formdata, { withcredentials: true, headers: { 'content-type': undefined }, transformrequest: angular.identity }).success(function (data) { if (data.success.tolowercase() == "true") { $scope.profiledetails.profilepicdataurl = data.profilepicdataurl; ngdialog.closeall(); } else { switch (data.success) { case "false": case "-1": $scope.profilepicerror = $scope.dynamicstrings.errormessage_erroruploadingfile; break; case "-2": $scope.profilepicerror = $scope.dynamicstrings.picture_filesizeexceed; break; case "-3": $scope.profilepicerror = $scope.dynamicstrings.profilepic_unsupportedfiletype; break; case "-4": $scope.profilepicerror = $scope.dynamicstrings.errormessage_invalidprofilepicname; break; } } }); },
mvc controller method found controller gets httppostedfilebase content length 0 .
[httppost] public actionresult uploadprofilepic(int clientid, int siteid, string sessionid, string iconid) { httppostedfilebase hpf = null; uploadprofilepicrequest request = new uploadprofilepicrequest(); request.clientid = clientid; request.siteid = siteid; request.sessionid = sessionid; myactionresponse response = new myactionresponse(); string erroruploading = string.empty; response.success = "-1"; var validextensions = new list<string> { "png", "gif", "jpg", "jpeg", "bmp", "tif" }; try { if (request.files.count > 0) { hpf = request.files[0] httppostedfilebase; } if (hpf != null && hpf.contentlength > 0) { string encodedpicture = string.empty; request.filesize = hpf.contentlength.tostring(); binaryreader br = new binaryreader(hpf.inputstream); } } catch (system.exception) { } return this.json(response); }
wiki
Comments
Post a Comment