2017-10-20 72 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中 –

相关问题