如何在C#中读取数据表中的二进制数据? [英] How to read binary data in to datatable in C# ?

查看:500
本文介绍了如何在C#中读取数据表中的二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用文件上传控件,我将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搜索并试了很多东西但没有用。

解决方案

files)id =fileChartOfAccounts />





java脚本代码客户端:



 


scope.getTheFiles = function(


files){
debugger;
angular.forEach (

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.

解决方案

files)" id="fileChartOfAccounts" />


java script code client side:


scope.getTheFiles = function (


files) { debugger; angular.forEach(


这篇关于如何在C#中读取数据表中的二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆