Download / Upload large files in C# Asp.Net
In web application download and upload are very common .
Earlier the file was small .500 kb text file , 500 kb image file ot doc file of maximum
1 mb . Now with enhancement of technology , normal images getting high
definition images , normal video files to high definition videos , the size of
this files are long . I can remember in 1997 , we use floppy disk to store
information , which maximum capacity was 1.44 Mb.Now days , a still photo comes with more than 1 Mb . However
, as developer we have to handle the situation with demand . Now I am showing ,
how we will upload & down load Large files
Upload large file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DownloadFile.aspx.cs" Inherits="DownloadFile"
%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<INPUT
type=file id=File1 name=File1 runat="server"
>
<asp:Button ID="btnUploadFile"
runat="server"
onclick="btnUploadFile_Click"
Text="Upload
File" />
</form>
</body>
</html>
protected void
btnUploadFile_Click(object sender, EventArgs e)
{
string FileName = "AAOHN Headquarters - August 10th Webinar
Video.wmv";
string PathName = @"C:\\Users\\abanerjee\\Desktop\\download_large_files\\LargeFile\\"
+ FileName;
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation =
Server.MapPath("DownLoad") + "\\" + fn;
try
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write("The
file has been uploaded.");
}
catch (Exception ex)
{
Response.Write("Error:
" + ex.Message);
}
}
----------------------web.config-----------------------------------------------
xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime
executionTimeout="90"
maxRequestLength="524288000"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
enableVersionHeader="true"
/>
</system.web>
</configuration>
Upload larger file is similar to upload file , one change is there , you have to just change
maxRequestLength value as per requirement , to handle 500 Mb files we have changes it to
524288000 .No other changes are required .
524288000 .No other changes are required .
Download a larger file
500Mb/1GB/2GB
-------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DownloadFile.aspx.cs" Inherits="DownloadFile"
%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:Button
ID="btnDownLoad"
runat="server"
onclick="btnDownLoad_Click"
Text="Down Load" />
</div>
</form>
</body>
</html>
protected void
btnDownLoad_Click(object sender, EventArgs e)
{
//here we have taken a sample file name and path
string FileName = "myfilename.wmv";
string PathName = "c://MyFiles/"+FileName;
//we are checking file exists or not
if (System.IO.File.Exists(PathName) == false)
{
return;
}
else
{
System.IO.Stream
myStream = null;
try
{
myStream =
new System.IO.FileStream
(path: PathName,
mode: System.IO.FileMode.Open,
share: System.IO.FileShare.Read,
access: System.IO.FileAccess.Read);
//
long lngFile =
myStream.Length;
long lngData =
lngFile;
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=" + FileName);
Response.AddHeader("Content-Length",
lngFile.ToString());
//
**************************************************
//looping through the streaming length
while (lngData >
0)
{
if (Response.IsClientConnected)
{
// 8KB
int
intBufferSize = 8 * 1024;
byte[]
bytBuffers =
new
System.Byte[intBufferSize];
int intTheBytesThatReallyHasBeenReadFromTheStream =
myStream.Read(buffer: bytBuffers,
offset: 0, count: intBufferSize);
Response.OutputStream.Write
(buffer: bytBuffers, offset: 0,
count:
intTheBytesThatReallyHasBeenReadFromTheStream);
Response.Flush();
lngData =
lngData -
intTheBytesThatReallyHasBeenReadFromTheStream;
}
else
{
lngData = -1;
}
}
}
catch { }
finally
{
if (myStream != null)
{
//Close the file.
myStream.Close();
myStream.Dispose();
myStream = null;
}
Response.Close();
}
}
}
No comments:
Post a Comment