我正在做一个将文件上传到 SQL Server 然后下载它的项目建议我一个链接或示例代码以选择多个文件并将其上传到 SQL Server
提前致谢...
您需要使用FileUpload.PostedFiles
属性
尝试使用以下代码:
foreach (HttpPostedFile upFile in FileUpload1.PostedFiles)
{
SaveFiles(upFile);
}
private void SaveFiles(HttpPostedFile fObj)
{
using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString
{
using(SqlCommand cmd = new SqlCommand(DatabaseQuery,con)) // set appropriate query
{
cmd.Parameters.AddWithValue("@data", ReadFile(fObj));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
private byte[] ReadFile(HttpPostedFile fObj2)
{
byte[] data = new Byte[fObj2.ContentLength];
fObj2.InputStream.Read(data, 0, file.ContentLength);
return data;
}
MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfiles.aspx
希望它有帮助。
请务必使用 AllowMultiple 属性(.Net 4.5 支持):
<asp:FileUpload ID="MyFileUpload" runat="server" AllowMultiple="true" />
您现在可以使用 ALT/STRG 选择要上传的多个文件。然后使用 Freelancer 的代码。