本文介绍了将大文件上载到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数可以将文件保存到SQL服务器。它在文件大约5mb以下时工作正常,但是大于那个并且在我点击提交后(最终触发此功能)它会出现400页错误。


private void btnUpload_Click (对象发送者,System.EventArgs e)


{


HttpPostedFile myFile = fileUpload.PostedFile;


byte [] myFileData = new byte [myFile.ContentLength];


myFile.InputStream.Read(myFileData,0,myFile.ContentLength);

>
string fileName = Path.GetFileName(myFile.FileName);


SaveFileToSQL(fileName,myFile.ContentType,ref myFileData);


}


私有字符串SaveFileToSQL(字符串fileName,字符串fileType,ref byte [] fileData)


{


SqlConnection myConnection = new SqlConnection(< connection details>)

SqlDataAdapter myDataAdapter = new SqlDataAdapter(" SELECT * FROM FileX",myConnection);


SqlCommandB uilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);


myConnection.Open();


DataSet myDataSet = new DataSet();


myDataAdapter.Fill(myDataSet," FileX");


DataTable myDataTable = myDataSet.Tables [" FileX"];


//将数据插入SQL中

DataRow myDataRow = myDataTable.NewRow();


myDataRow [" ; FileName"] = fileName;


myDataRow [" FileSize"] = fileData.Length;


myDataRow [" ContentType"] = fileType;


myDataRow [" FileData"] = fileData;


myDataTable.Rows.Add(myDataRow);


myDataAdapter.Update(myDataSet," FileX");


myConnection.Close();


}

-


Steve Evans

电子邮件服务

SDSU基金会

(619)594-0708

解决案


您可以在web.config文件中添加或修改以下部分:


< configuration>

< system.web>

< httpRuntime maxRequestLength =" 4096" />

< /system.web>

< / configuration>


上述值(4096 KB)是默认的最大上传文件大小。


-

我希望这会有所帮助,

Steve C. Orr,MCSD, MVP



" - 史蒂夫 - " < SE **** @ foundation.sdsu.edu>在消息新闻中写道:un ************** @ TK2MSFTNGP12.phx.gbl ...

我有以下函数可以将文件保存到一个SQL服务器。它在文件大约5mb以下时工作正常,但是大于那个并且在我点击提交后(最终触发此功能)它会出现400页错误。


private void btnUpload_Click (对象发送者,System.EventArgs e)


{


HttpPostedFile myFile = fileUpload.PostedFile;


byte [] myFileData = new byte [myFile.ContentLength];


myFile.InputStream.Read(myFileData,0,myFile.ContentLength);

>
string fileName = Path.GetFileName(myFile.FileName);


SaveFileToSQL(fileName,myFile.ContentType,ref myFileData);


}


私有字符串SaveFileToSQL(字符串fileName,字符串fileType,ref byte [] fileData)


{


SqlConnection myConnection = new SqlConnection(< connection details>)

SqlDataAdapter myDataAdapter = new SqlDataAdapter(" SELECT * FROM FileX",myConnection);


SqlCommandB uilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);


myConnection.Open();


DataSet myDataSet = new DataSet();


myDataAdapter.Fill(myDataSet," FileX");


DataTable myDataTable = myDataSet.Tables [" FileX"];


//将数据插入SQL中

DataRow myDataRow = myDataTable.NewRow();


myDataRow [" ; FileName"] = fileName;


myDataRow [" FileSize"] = fileData.Length;


myDataRow [" ContentType"] = fileType;


myDataRow [" FileData"] = fileData;


myDataTable.Rows.Add(myDataRow);


myDataAdapter.Update(myDataSet," FileX");


myConnection.Close();


}

-


Steve Evans

电子邮件服务

SDSU基金会

(619)594-0708


好的,解决了这个问题。但是现在当我上传一个500 + mb文件时,我在Web服务器上的内存不足。有没有更好的方法来实现这一点,我可以流式传输数据,而不是创建如此庞大的byte []变量。


-


Steve Evans

电子邮件服务

SDSU基金会

(619)594-0708

" Steve C 。奥尔[MVP,MCSD]" <圣*** @ Orr.net>在消息新闻中写道:%2 *************** @ TK2MSFTNGP11.phx.gbl ...

您可以在网络中添加或修改以下部分.config文件:


< configuration>

< system.web>

< httpRuntime maxRequestLength =" 4096" ; />

< /system.web>

< / configuration>


上述值(4096 KB)是默认的最大上传文件大小。


-

我希望这会有所帮助,

Steve C. Orr,MCSD, MVP



" - 史蒂夫 - " < SE **** @ foundation.sdsu.edu>在消息新闻中写道:un ************** @ TK2MSFTNGP12.phx.gbl ...

我有以下函数可以将文件保存到一个SQL服务器。它在文件大约5mb以下时工作正常,但是大于那个并且在我点击提交后(最终触发此功能)它会出现400页错误。


private void btnUpload_Click (对象发送者,System.EventArgs e)


{


HttpPostedFile myFile = fileUpload.PostedFile;


byte [] myFileData = new byte [myFile.ContentLength];


myFile.InputStream.Read(myFileData,0,myFile.ContentLength);

>
string fileName = Path.GetFileName(myFile.FileName);


SaveFileToSQL(fileName,myFile.ContentType,ref myFileData);


}


私有字符串SaveFileToSQL(字符串fileName,字符串fileType,ref byte [] fileData)


{


SqlConnection myConnection = new SqlConnection(< connection details>)

SqlDataAdapter myDataAdapter = new SqlDataAdapter(" SELECT * FROM FileX",myConnection);


SqlCommandB uilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);


myConnection.Open();


DataSet myDataSet = new DataSet();


myDataAdapter.Fill(myDataSet," FileX");


DataTable myDataTable = myDataSet.Tables [" FileX"];


//将数据插入SQL中

DataRow myDataRow = myDataTable.NewRow();


myDataRow [" ; FileName"] = fileName;


myDataRow [" FileSize"] = fileData.Length;


myDataRow [" ContentType"] = fileType;


myDataRow [" FileData"] = fileData;


myDataTable.Rows.Add(myDataRow);


myDataAdapter.Update(myDataSet," FileX");


myConnection.Close();


}

-


Steve Evans

电子邮件服务

SDSU基金会

(619)594-0708


I have the following function that is supposed to save a file to a SQL server. It works fine when the file is under about 5mb, but bigger than that and after I hit submit (which eventually fires off this function) it get a 400 page error.

private void btnUpload_Click(object sender, System.EventArgs e)

{

HttpPostedFile myFile = fileUpload.PostedFile;

byte[] myFileData = new byte[myFile.ContentLength];

myFile.InputStream.Read(myFileData, 0, myFile.ContentLength);

string fileName = Path.GetFileName(myFile.FileName);

SaveFileToSQL(fileName, myFile.ContentType, ref myFileData);

}

private string SaveFileToSQL(string fileName, string fileType, ref byte[] fileData)

{

SqlConnection myConnection = new SqlConnection(<connection details>)
SqlDataAdapter myDataAdapter = new SqlDataAdapter("SELECT * FROM FileX", myConnection);

SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);

myConnection.Open();

DataSet myDataSet = new DataSet();

myDataAdapter.Fill(myDataSet, "FileX");

DataTable myDataTable = myDataSet.Tables["FileX"];

//insert data into SQL

DataRow myDataRow = myDataTable.NewRow();

myDataRow["FileName"] = fileName;

myDataRow["FileSize"] = fileData.Length;

myDataRow["ContentType"] = fileType;

myDataRow["FileData"] = fileData;

myDataTable.Rows.Add(myDataRow);

myDataAdapter.Update(myDataSet, "FileX");

myConnection.Close();

}
--

Steve Evans
Email Services
SDSU Foundation
(619) 594-0708

解决方案




这篇关于将大文件上载到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:31