AngularJS 1.5文件上传在IE/Chrome中调试时工作
我已经编写了从angularjs获取base64数据并传递到.Net进行二进制转换的代码。它在我的系统中运行良好。我已将代码部署到服务器中。字节数组将变为空,但警报消息将成功。但若我把调试器放在控制台中,那个么它可以很好地工作,因为字节内容正在运行。不明白我做错了什么。首先,我在浏览时将fileobject放入rootscope变量。点击上传按钮解析文件并保存到数据库中。请帮帮我。 HTML代码AngularJS 1.5文件上传在IE/Chrome中调试时工作,angularjs,asp.net-web-api,file-upload,binary,Angularjs,Asp.net Web Api,File Upload,Binary,我已经编写了从angularjs获取base64数据并传递到.Net进行二进制转换的代码。它在我的系统中运行良好。我已将代码部署到服务器中。字节数组将变为空,但警报消息将成功。但若我把调试器放在控制台中,那个么它可以很好地工作,因为字节内容正在运行。不明白我做错了什么。首先,我在浏览时将fileobject放入rootscope变量。点击上传按钮解析文件并保存到数据库中。请帮帮我。 HTML代码 <div class="col-sm-4"> <input filestyle
<div class="col-sm-4">
<input filestyle="" file-model = "signatureImage" name="signatureImage" type="file" accept=".png" />
</div>
<div class="col-sm-2">
<button type="button" ng-click="apc.uploadFile()" class="btn btn-info btn-xs"><span class="icon-cloud-upload mr"></span>Upload
</button>
</div>
net代码如下所示
[HttpPost]
public DataSet USA(apc.ImageModel imageModel)
{
ResultSet dsSaveDoc = null;
imageModel.Bytes = Convert.FromBase64String(imageModel.FileType);
imageModel.FileType = "image/png";
//DB Calling
}
现在,这里的imageModel.Bytes
将在不放置调试器的情况下变为NULL,若我放置调试器,它在服务器中工作正常
请帮助我……直接发送文件比将文件转换为base64更好。如果必须使用base64,FormData API会更高效。请参阅将其结果存储在$rootScope上的指令是不明智的。考虑使用与NGMODEL控制器一起工作的指令。看见另请参阅。因为从APC控制器作用域,我并没有从指令作用域的设置值获取文件对象。我已经检查了这两个场景的作用域id是否不同,所以我使用了rootscope来避免它。但当我将调试器放在“vm.uploadFile”中时,它在Chrome和IE中都能正常工作。但删除调试器时,相同的代码会在存储过程中发送NULL…我正在将BLOB(varbinary)保存到DB.Read中。新的AngularJS开发人员通常没有意识到
ng repeat
、ng switch
、ng view
、ng include
和ng if
都会创建新的子范围,因此当涉及到这些指令时,问题往往会出现。
(function () {
'use strict';
angular
.module('ba')
.controller('APC', APC);
APC.$inject = ['$http', '$scope', 'ngDialog', 'DTOptionsBuilder', '$filter', '$rootScope', '$timeout', '$localStorage' ];
function APC($http, $scope, ngDialog, DTOptionsBuilder, $filter, $rootScope, $timeout, $localStorage) {
vm.uploadFile= function () {
var UserInfoID = sessionStorage.getItem('UserInfoId');
var DataVariable = "signatureImage_" + UserInfoID;
var file = $rootScope[DataVariable];
var UserName = vm.NewPersonInfo.UserName;
var reader = new FileReader();
reader.readAsDataURL(file);
var fileData = reader.result;
fileData = fileData.replace("data:image/png;base64,", "");
var uploadData = {
Name: file.name,
FileType: fileData,
UserInfoID: UserName
}
$http.post('api/AP/USA', uploadData)
.then(
function (response) {
if (response != null && response.data != null) {
alert("Digital Successfully Uploaded.");
}
else {
alert("Digital Successfully Upload Failed!!!");
}
});
};
}
})();
[HttpPost]
public DataSet USA(apc.ImageModel imageModel)
{
ResultSet dsSaveDoc = null;
imageModel.Bytes = Convert.FromBase64String(imageModel.FileType);
imageModel.FileType = "image/png";
//DB Calling
}