将大文件上载到SQL [英] Uploading Large File to SQL

查看:65
本文介绍了将大文件上载到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

解决案
喜史蒂夫,


默认情况下,ASP.NET有一个4MB的上传发帖限制。这可以通过编辑< httpRuntime>在machine.config或web.config中更改。标记并将maxRequestLength设置为8192左右(这将为您提供8 MB的上传限制)。 maxRequestLength属性接受任何数字,以字节表示。


此外,您还需要将executionTimeout属性增加到300(5分钟)左右。默认情况下,该值为90秒,除非您在快速LAN上,否则通常不足以上传5 MB文件。 executionTimeout属性也将接受任何数字,表示为秒。


HTH,


Kyril


- 史蒂夫 - " < 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


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


< configuration>

< system.web>

< httpRuntime maxRequestLength =" 4096" />

< /system.web>

< / configuration>


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


-

我希望这会有所帮助,

Steve C. Orr,MCSD, MVP
http://Steve.Orr.net


" - 史蒂夫 - " < 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
http://Steve.Orr.net


" - 史蒂夫 - " < 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

解决方案

Hi Steve,

By default, ASP.NET has a 4MB upload posting limit. This can be changed in either machine.config or in the web.config by editing the <httpRuntime> tag and setting the maxRequestLength to something around 8192 (that will give you an 8 MB upload limit). The maxRequestLength attribute accepts any number, expressed in bytes.

Also, you will want to increase the executionTimeout attribute to something around 300 (5 minutes). By default, the value is 90 seconds, which is usually not long enough to upload a 5 MB file unless you are on a fast LAN. The executionTimeout attribute will also accept any number, expressed as seconds.

HTH,

Kyril

"- Steve -" <se****@foundation.sdsu.edu> wrote in message news:un**************@TK2MSFTNGP12.phx.gbl...
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


You can add or modify the following section in your web.config file:

<configuration>
<system.web>
<httpRuntime maxRequestLength="4096" />
</system.web>
</configuration>

The above value (4096 KB) is the default maximum upload file size.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"- Steve -" <se****@foundation.sdsu.edu> wrote in message news:un**************@TK2MSFTNGP12.phx.gbl...
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


Okay that fixed the problem. However now when I''m upload a 500+mb file I run out of memory on the web server. Is there a better way to do this where I can stream the data in instead of creating such a huge byte[] variable.

--

Steve Evans
Email Services
SDSU Foundation
(619) 594-0708
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message news:%2***************@TK2MSFTNGP11.phx.gbl...
You can add or modify the following section in your web.config file:

<configuration>
<system.web>
<httpRuntime maxRequestLength="4096" />
</system.web>
</configuration>

The above value (4096 KB) is the default maximum upload file size.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"- Steve -" <se****@foundation.sdsu.edu> wrote in message news:un**************@TK2MSFTNGP12.phx.gbl...
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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