问题描述
通过使用文件上传控件,我将Excel数据作为二进制数据读取。
现在我需要将二进制数据转换为数据表以将其保存在数据库中。
请帮忙。
html
< input type =filename =fileChartOfAccountsng-files = getTheFiles($ files)id =fileChartOfAccounts/>
java脚本代码客户端:
$
$ $ $ $ $ $ $ $ $ $ $ $
angular.forEach($ files,function(file){
$ scope.filesUploaded.push(file);
});
$ scope.UploadFilesEnabled = true;
$ scope。$ apply();
};
if($ scope.filesUploaded。长度> 0){
$(#ModalProcessingSharefileUpload)。modal(show);
var request = {
method:'POST',
url:baseUrl +'api / fileuploadnotedtransaction / UploadFiles',
data:formdata,
headers:{
'Content-Type':undefined
}
};
//发送文件。
$ http(请求)
.success(函数(d){
//调试器;
$ scope.clear();
$(#ModalProcessingSharefileUpload ).modal(hide);
})
.error(function(err){
// debugger;
$ scope.clear();
$(#ModalProcessingSharefileUpload)。modal(hide);
});
}
控制器中的服务器端
[HttpPost()]
public void UploadFiles()
{
System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
for(int iCnt = 0; iCnt< = hfc.Count - 1; iCnt ++)
{
System.Web.HttpPostedFile hpf = hfc [iCnt];
BinaryReader b = new BinaryReader(hpf.InputStream);
byte [] binData = b.ReadBytes(hpf.ContentLength);
MemoryStream memoryStreamOfFile = new MemoryStream(binData);
BinaryFormatter formatter = new BinaryFormatter();
memoryStreamOfFile.Seek(0,SeekOrigin.Begin);
DataTable dt =(DataTable)formatter.Deserialize(memoryStreamOfFile);
}
}
我的尝试:
我用Google搜索并试了很多东西但没有用。
By using file upload control i'm reading Excel data as binary data.
Now i need to convert binary data into datatable to save it in DB.
Please help.
html
<input type="file" name="fileChartOfAccounts" ng-files="getTheFiles($files)" id="fileChartOfAccounts" />
java script code client side:
$scope.getTheFiles = function ($files) { debugger; angular.forEach($files, function (file) { $scope.filesUploaded.push(file); }); $scope.UploadFilesEnabled = true; $scope.$apply(); };
if ($scope.filesUploaded.length > 0) { $("#ModalProcessingSharefileUpload").modal("show"); var request = { method: 'POST', url: baseUrl + 'api/fileuploadnotedtransaction/UploadFiles', data: formdata, headers: { 'Content-Type': undefined } }; // SEND THE FILES. $http(request) .success(function (d) { //debugger; $scope.clear(); $("#ModalProcessingSharefileUpload").modal("hide"); }) .error(function (err) { //debugger; $scope.clear(); $("#ModalProcessingSharefileUpload").modal("hide"); }); }
Server Side in controller
[HttpPost()] public void UploadFiles() { System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++) { System.Web.HttpPostedFile hpf = hfc[iCnt]; BinaryReader b = new BinaryReader(hpf.InputStream); byte[] binData = b.ReadBytes(hpf.ContentLength); MemoryStream memoryStreamOfFile = new MemoryStream(binData); BinaryFormatter formatter = new BinaryFormatter(); memoryStreamOfFile.Seek(0, SeekOrigin.Begin); DataTable dt = (DataTable)formatter.Deserialize(memoryStreamOfFile); } }
What I have tried:
I googled it and tried many things but none worked.
这篇关于如何在C#中读取数据表中的二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!