2017-10-20 62 views
0

嗨,我想存儲在SQL數據庫表中的Excel文件,但我有我的代碼在服務器端C#一些麻煩。 所以事情是,我採取了所有我需要的值,但其中一個是實際的Excel文件。所以我有兩個問題:
1)我可以通過使用varbinary(max)數據類型在表中正確存儲excel的文件。
2)如果是這樣,我如何通過ajax將該文件傳遞給我的服務器代碼?正如我趕上用戶在js文件我不是很肯定的我可以處理它在C#
這裏是我的代碼:在AJAX和ASP.NET的SQL數據庫中插入Excel文件

<script> 
 

 

 

 
     function tryme() { 
 
      var modelx = new Object(); 
 

 
      var depS = document.getElementById("resultDep"); 
 
      var catS = document.getElementById("resultCat"); 
 
      var schemS = document.getElementById("schemes"); 
 

 
      var wiw = document.getElementById("wiw").value; 
 
      modelx.wiw = wiw; 
 

 
      var nm = document.getElementById("namePro").value; 
 
      modelx.name = nm; 
 

 
      var dp = depS.options[depS.selectedIndex].value; 
 
      modelx.dep = dp; 
 

 
      var ct = catS.options[catS.selectedIndex].value; 
 
      modelx.cat = ct; 
 

 
      var sch = schemS.options[schemS.selectedIndex].value; 
 
      modelx.schem = sch; 
 
      var myFile = $('#fileinput').prop('files'); 
 

 
      modelx.f = myFile; 
 
      alert("../"+ modelx.name +"/" + modelx.cat + "/" + modelx.dep + "/" + modelx.schem 
 
       + "/" + modelx.wiw + "/" +modelx.f); 
 

 

 
      $.ajax({ 
 
       type: "POST", 
 
       dataType: "json", 
 
       contentType: "application/json; charset=utf-8", 
 
       url: '@Url.Action("CreateRequest", "Home")', 
 
       data: "{'Name':'" + modelx.name + "', 'Category':'" + modelx.cat + "',"+ 
 
        "'Department':'" + modelx.dep + "' ," + "'Wiw':'" + modelx.wiw + "',"+ 
 
        "'Scheme':'" + modelx.schem + "' ," + "'File':'" + modelx.f + "'" 
 
        + "}", 
 
       success: function (Record) { 
 
       }, 
 
       Error: function (textMsg) { 
 

 
        alert("something got wrong"); 
 
       } 
 
      }); 
 

 

 

 

 
     //end function 
 
     } 
 
     
 
     </script>
<script src="~/Scripts/jquery-1.10.2.js"></script> 
 
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script> 
 

 
<label>Departments:</label><br /><select id="resultDep" onchange="GetCategories()"> 
 
    <option>-----</option> 
 
</select> 
 
<br /><br /> 
 
<label>Categories:</label><br /><select id="resultCat"></select> 
 
<br /><br /> 
 
<label>Scheme:</label><br /> 
 
<select id="schemes" required></select> 
 
<br /> 
 
<br /> 
 
<p>Nombre: @Session["Name"]</p> 
 
<!--   <p>Area: Innovación tecnológica</p>--> 
 
<p>Tipo de usuario: @Session["Role"]</p> 
 
<p>E-mail: @Session["Correo"]</p> 
 
<p >WIW:@Session["WiW"]</p><br /> 
 

 

 
<form action="" method="post"> 
 
    <input type="text" class="hidden" id="wiw" value="@Session["WiW"]" /> 
 
    <input type="text" id="namePro" value="" /> 
 
    <br /> 
 
     <input type="file" id="fileinput" /> 
 

 
    <input type="submit" onclick="tryme()" value="click" /> 
 
</form>

服務器端代碼

C# 
 

 
public static bool CreateRequest(string Name, int Category, 
 
      int Department, string Wiw, int Scheme, 
 
      file? myfile //just here i want to pass that file but i don't know how 
 
      ) 
 
     { 
 
     string cs = ""; 
 

 
      using (SqlConnection con = new SqlConnection(cs)) 
 
      { 
 

 
       using (SqlCommand cmd = new SqlCommand(cs, con)) 
 
       { 
 
       // my code...

回答

1

在在客戶端,改變從應用/ JSON內容類型爲multipart/form-數據

C#的一面,看進級HttpPostedFileBase

在SQL Server端,我建議尋找到SQL服務器文件流數據類型

+0

嗨,我查找HttpPostedFileBase我發現了一些方法來存儲文件的**路徑**和存儲在服務器的文件夾中的文件,但我想存儲我的文件在SQL基地使用queryi' m使用** varbinary **數據類型來存儲在sql中 –

相關問題