How to upload big file to DB with webpage?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I want to upload the file by webpage to SQL Server.

When I upload the small file, it is work.
But, when I upload big file(ex. 40~50 MB), it isn't work;
the Browser displays the following error message:
"The webpage consulted at present is unable to use.
Websites may happen the technological question or you need to adjust the
browser to establish ."

I test it,
reading file to byte array is ok, but import byte array to DB is error.
I add "Connect Timeout=300", it still has this error.

What do this error mean?
And how to solve it?

Thanks for your help.
 
Uploads done through an HTTP POST are subject to the maximum request size.
In most cases this is 4096 k (4 megs).

If you have control over the server you can change the MaxRequestSize
setting in machine.config. if not you'll have to collaborate with whoever
runs the server to create a page that enables you to send chunks of a file
in smaller portions.

If it's not a POST issue then I don't know what to suggest.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
This problem can be due to two different cause:
1. Your file is not fully being uploaded.
2. Your logic to update database server is taking too long.
Let me address prblem number one. Solution for this problem lies in
web.config file. Add following section:

<httpRuntime
maxRequestLength="40000"
executionTimeout="1800"
/>

maxRequestLength is request size in kb and executionTimeout is in seconds.
Hope this makes sence to you.
To find out if the problem is of second type, you divide your process into
two. First only try to upload the file, if it is successful, that means your
database update logic is taking long. If this is the case, post your
database update logic here, we will optimize it.

-Praveen
 
Hi,


Put this in your web.config

<httpRuntime maxRequestLength="100000" ></httpRuntime>



Cheers,
 
I check <httpRuntime> setting, it already set.
Whatever I set how big to maxRequestLength and executionTimeout, it still
can't work.

I upload this big file(about 5xMB) to server's hard disk, it could upload
it.
But, I upload this file into DB, it couldn't do it.

My codes are following codes.
Thanks.

--------------------------
UploadFile.aspx:
--------------------------
<%@ Page language="c#" Codebehind="UploadFile.aspx.cs"
AutoEventWireup="false" Inherits="UploadFile.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Label ID="msg" Runat="server" /><br>
File Path: <input type="file" id="txtPath" runat="server"><p></p>
<asp:Button id="btnSave" runat="server" Text="Save"></asp:Button>
</form>
</body>
</HTML>

--------------------------
UploadFile.aspx.cs:
--------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

namespace UploadFile
{
/// <summary>
/// WebForm1 çš„æ‘˜è¦æè¿°ã€‚
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile txtPath;
protected System.Web.UI.WebControls.Label msg;
protected System.Web.UI.WebControls.Button btnSave;

private void Page_Load(object sender, System.EventArgs e)
{
// 在這裡放置使用者程å¼ç¢¼ä»¥åˆå§‹åŒ–ç¶²é 
msg.Text="";
}

#region Web Form 設計工具產生的程å¼ç¢¼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼å«ã€‚
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// æ­¤ç‚ºè¨­è¨ˆå·¥å…·æ”¯æ´æ‰€å¿…須的方法 - 請勿使用程å¼ç¢¼ç·¨è¼¯å™¨ä¿®æ”¹
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnSave_Click(object sender, System.EventArgs e)
{
if(txtPath.Value.Length > 0)
{
try
{
msg.Text="";

FileStream f=new FileStream(txtPath.Value, FileMode.Open);
BinaryReader br=new BinaryReader(f);
byte[] myPic=br.ReadBytes((int)f.Length);
br.Close();

msg.Text="File->Byte[]: OK";
//return;

try
{
string conn_str="server=DV-PC-377;database=Test;uid=test;pwd=test;";
SqlConnection conn = new SqlConnection(conn_str);
conn.Open();

SqlCommand cmd=new SqlCommand("Insert Into MyPicture(PicPath,Pic)
Values(@Path, @Pic)", conn);
cmd.Parameters.Add("@Path", SqlDbType.VarChar,
50).Value=txtPath.Value;
cmd.Parameters.Add("@Pic", SqlDbType.Image).Value=myPic;
cmd.ExecuteNonQuery();

conn.Close();

Response.Write(conn.State.ToString()+"<br>");
msg.Text="Byte[]->DB: OK";

return;
}
catch(Exception exc2)
{
msg.Text="Byte[]->DB: "+exc2.Message;
}
}
catch(Exception exc)
{
msg.Text="File->Byte[]: "+exc.Message;
}
}
}

}
}

--------------------------
Web.config:
--------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<system.web>

<httpRuntime maxRequestLength="400000" executionTimeout="1800" />

<!-- å‹•æ…‹åµéŒ¯ç·¨è­¯
設定 compilation debug="true" 以啟用 ASPX åµéŒ¯ã€‚å¦å‰‡ï¼Œå°‡é€™å€‹å€¼è¨­å®š
為
false å°‡å¯å¢žé€²é€™å€‹æ‡‰ç”¨ç¨‹å¼çš„ Runtime 效能。
設定 compilation debug="true" 會將åµéŒ¯ç¬¦è™Ÿ (.pdb 資訊) æ’入編譯的
ç¶²é ã€‚
這樣將會建立較大的檔案並使執行速度變慢。
一般åªåœ¨åµéŒ¯æ™‚將這個值設為 true,其他情æ³éƒ½è¨­ç‚º false。
如需詳細資訊,請åƒé–±
åµéŒ¯ ASP.NET 檔案相關的文件。
-->
<compilation
defaultLanguage="c#"
debug="true"
/>

<!-- 自訂錯誤訊æ¯
設定 customErrors mode="On" 或 "RemoteOnly" 來啟用自訂錯誤訊æ¯ï¼Œ
"Off" 則為åœç”¨ã€‚
è«‹å°æ¯å€‹æ‚¨è¦è™•ç†çš„éŒ¯èª¤ï¼ŒåŠ å…¥ç›¸å°æ‡‰çš„ <error> 標記。

"On" æ°¸é é¡¯ç¤ºè‡ªè¨‚ (易讀) 訊æ¯ã€‚
"Off" æ°¸é é¡¯ç¤ºè©³ç´°çš„ ASP.NET 錯誤資訊。
"RemoteOnly" 僅顯示自訂 (易讀) 訊æ¯çµ¦ä¸åœ¨å€åŸŸ Web 伺æœå™¨ä¸Šçš„使用
者。
é‡å°å®‰å…¨æ€§ç›®çš„,建議您使用這項設定,
é€™æ¨£å­æ‚¨å°±ä¸æœƒå°‡æ‡‰ç”¨ç¨‹å¼çš„詳細資訊顯示給é ç«¯ç”¨æˆ¶ç«¯ã€‚
-->
<customErrors
mode="RemoteOnly"
/>

<!-- é©—è­‰
這個倿®µå°‡è¨­å®šæ‡‰ç”¨ç¨‹å¼çš„驗證原則。å¯èƒ½çš„æ¨¡å¼åŒ…括 "Windows"ã€
"Forms"ã€"Passport" 與 "None"

"None" 沒有執行任何的驗證。
"Windows" IIS 會根據其é‡å°æ‡‰ç”¨ç¨‹å¼æ‰€åšçš„設定來執行驗證 (åŸºæœ¬ã€æ‘˜è¦
弿ˆ–æ•´åˆå¼ Windows é©—è­‰)
IIS 的匿åå­˜å–功能必須åœç”¨ã€‚
"Forms" æä¾›è‡ªè¨‚表單 (ç¶²é ) 讓使用者輸入他們的憑證,
然後在應用程å¼ä¸­é©—證其憑證。使用者憑證 Token 儲存在 Cookie 中。
"Passport" 驗證是經由 Microsoft 所æä¾›çš„中央驗證æœå‹™è€ŒåŸ·è¡Œï¼Œ
此中央驗證æœå‹™å¯æ›¿æˆå“¡ç¶²ç«™æä¾›å–®ä¸€ç™»å…¥èˆ‡æ ¸å¿ƒè¨­å®šæª”æœå‹™ã€‚
-->
<authentication mode="Windows" />

<!-- 授權
這個倿®µå°‡è¨­å®šæ‡‰ç”¨ç¨‹å¼çš„æŽˆæ¬ŠåŽŸå‰‡ã€‚æ‚¨å¯ä»¥å…許或拒絕ä¸åŒä½¿ç”¨è€…或角色
å­˜å–æ‡‰ç”¨ç¨‹å¼è³‡æºã€‚
è¬ç”¨å­—å…ƒ: "*" 代表所有的人ã€"?" 代表匿å (未驗證的) 使用者。
-->

<authorization>
<allow users="*" /> <!-- å…許所有使用者 -->
<!-- <allow users="[使用逗號分隔的使用者清單]"
roles="[使用逗號分隔的角色清單]"/>
<deny users="[使用逗號分隔的使用者清單]"
roles="[使用逗號分隔的角色清單]"/>
-->
</authorization>

<!-- 應用程å¼å±¤ç´šè¿½è¹¤è¨˜éŒ„
應用程å¼å±¤ç´šè¿½è¹¤å•Ÿç”¨æ‡‰ç”¨ç¨‹å¼ä¸­æ¯ä¸€é é¢çš„追蹤記錄檔輸出。
設定 trace enabled="true" 將啟用應用程å¼è¿½è¹¤è¨˜éŒ„。如果
pageOutput="true",追蹤資訊將顯示
在æ¯ä¸€é é¢çš„下方。此外,您也å¯ä»¥å¾ž Web 應用程å¼çš„æ ¹ç›®éŒ„é€éŽç€è¦½
"trace.axd" é é¢çš„æ–¹å¼ä¾†æª¢è¦–
應用程å¼çš„追蹤記錄檔。
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>

<!-- 工作階段狀態設定
根據é è¨­ï¼ŒASP.NET 會使用 Cookie ä¾†è­˜åˆ¥å“ªäº›è¦æ±‚是屬於æŸå€‹ç‰¹å®šå·¥ä½œéšŽ
段。
如果無法使用 Cookie,您也å¯ä»¥å°‡å·¥ä½œéšŽæ®µè­˜åˆ¥é …加入到 URL 來追蹤工作
階段。
è‹¥è¦åœç”¨ Cookie,請設定 sessionState cookieless="true"。
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data
source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>

<!-- å…¨çƒåŒ–設定
æ­¤å€æ®µç”¨ä¾†è¨­å®šæ‡‰ç”¨ç¨‹å¼å…¨çƒåŒ–é¸é …。
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>

</system.web>

</configuration>
 
Web form is not going to wait for 40mb byte[] load to complete. This method
is going to have very bad performance result when multiple users accessing
different services on your server. I suggest a logic change than a technical
solution. Divide the process into two.
1. Upload the file, using your present method, to a pre-defined folder on
the server.
2. Write a separate program to load all the files on that folder to the
database server and delete them.
According to your requirement, (ie, how fast you need the data on the
database)
a. run this program as scheduled task (may be at night)
b. or run it as a server service, using FileSystemWatcher on that folder,
to load files as they arrive.

If you need the user to be notified how successful the upload were, may be
you can collect the email address of the user and send status emails from
your upload program.

I suggest this program to be run from a separate computer considering amount
of memory it will require.

-Praveen
 
Thanks.
I think Server's RAM seems to be larger.
But, when the RAM is 1GB, it still can't save bigger file into SQL Server.
I test that it only upload about 4xMB file into SQL Server.

Does it use the memory of the Web Server when user upload the file?
Does it use the memory of the Web Server when the uploaded the file( in the
memory(byte array) ) saves into DB?

Thanks again.
 
for a short period, untill SaveAs() method finish its work. After that when
you load the data to byte array, it is again stored on server memory.
Having seen your question on the news group, I decided to post my past
experiance of solving a smilar problem in uploading file. Only difference is
that my file was MS Access (.mdb) database file. I have used slightly
different approach here. My server now is receiving upto 20 MB files using
this method. May be my experince help you with more ideas.

http://www.kpraveen.com/uploadMdb2Sql.htm

-Praveen

Grace said:
Thanks.
I think Server's RAM seems to be larger.
But, when the RAM is 1GB, it still can't save bigger file into SQL Server.
I test that it only upload about 4xMB file into SQL Server.

Does it use the memory of the Web Server when user upload the file?
Does it use the memory of the Web Server when the uploaded the file( in the
memory(byte array) ) saves into DB?

Thanks again.



Praveen said:
Web form is not going to wait for 40mb byte[] load to complete. This method
is going to have very bad performance result when multiple users accessing
different services on your server. I suggest a logic change than a technical
solution. Divide the process into two.
1. Upload the file, using your present method, to a pre-defined folder on
the server.
2. Write a separate program to load all the files on that folder to the
database server and delete them.
According to your requirement, (ie, how fast you need the data on the
database)
a. run this program as scheduled task (may be at night)
b. or run it as a server service, using FileSystemWatcher on that folder,
to load files as they arrive.

If you need the user to be notified how successful the upload were, may be
you can collect the email address of the user and send status emails from
your upload program.

I suggest this program to be run from a separate computer considering amount
of memory it will require.

-Praveen
 
Back
Top