.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share

 Subscribe to Articles

Upload Large Files in SQL Server in an ASP.NET web application

Posted By :Nikhil Kumar      Posted Date :02/02/2010   Points :25   Category: ASP.NET    URL: http://www.dotnetask.blog.co.in

Upload Large Files in SQL Server in an ASP.NET web application: . In this article we learn how to upload large files in sql sever database by using asp.net file upload control
    


Upload Large Files in SQL in an ASP.NET web application:

In this article we learn how to upload large files in sql sever database by using asp.net file upload control

FileUploadLarge.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
const string conString = @"Server=SAI;Integrated Security=True; initial catalog=master";
void btnAdd_Click(Object s, EventArgs e)
{
if (upFile.HasFile)
{
if (CheckFileType(upFile.FileName))
{
AddFile(upFile.FileName, upFile.FileContent);
rptFiles.DataBind();
}
}
}
bool CheckFileType(string fileName)
{
return Path.GetExtension(fileName).ToLower() == ".doc";
}
void AddFile(string fileName, Stream upload)
{
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("INSERT Files (FileName) Values (@FileName);" + "SELECT @Identity = SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("@FileName", fileName);
SqlParameter idParm = cmd.Parameters.Add("@Identity", SqlDbType.Int);
idParm.Direction = ParameterDirection.Output;
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
int newFileId = (int)idParm.Value;
StoreFile(newFileId, upload, con);
}
}
void StoreFile(int fileId, Stream upload, SqlConnection connection)
{
int bufferLen = 8040;
BinaryReader br = new BinaryReader(upload);
byte[] chunk = br.ReadBytes(bufferLen);
SqlCommand cmd = new SqlCommand("UPDATE Files SET FileBytes=@Buffer WHERE Id=@FileId", connection);
cmd.Parameters.AddWithValue("@FileId", fileId);
cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;
cmd.ExecuteNonQuery();
SqlCommand cmdAppend = new SqlCommand("UPDATE Files SET FileBytes .WRITE(@Buffer, NULL, 0) WHERE Id=@FileId", connection);
cmdAppend.Parameters.AddWithValue("@FileId", fileId);
cmdAppend.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen);
chunk = br.ReadBytes(bufferLen);
while (chunk.Length > 0)
{
cmdAppend.Parameters["@Buffer"].Value = chunk;
cmdAppend.ExecuteNonQuery();
chunk = br.ReadBytes(bufferLen);
}
br.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FileUpload Large</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblFile"
Text="Word Document:"
AssociatedControlID="upFile"
Runat="server" />
<asp:FileUpload
id="upFile"
Runat="server" />
<asp:Button
id="btnAdd"
Text="Add Document"
OnClick="btnAdd_Click"
Runat="server" />
<hr />
<asp:Repeater
id="rptFiles"
DataSourceID="srcFiles"
Runat="server">
<HeaderTemplate>
<ul class="fileList">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink
id="lnkFile"
Text='<%#Eval("FileName")%>'
NavigateUrl='<%#Eval("Id", "~/FileHandlerLarge.ashx?id={0}")%>'
Runat="server" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource id="srcFiles" ConnectionString="Server=SAI;Integrated Security=True; Initial catalog=master;" SelectCommand="SELECT Id,FileName FROM Files"
runat="server" />
</div>
</form>
</body>
</html>

Now add a .ashx file and use this code in it

 

<%@ WebHandler Language="C#" Class="FileHandlerLarge" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class FileHandlerLarge : IHttpHandler {
const string conString = @"Server=SAI;Integrated Security=True;Initial catalog= master";
public void ProcessRequest (HttpContext context) 
{context.Response.Buffer = false;
context.Response.ContentType = "application/msword";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("SELECT FileBytes FROM Files WHERE Id=@Id", con);
cmd.Parameters.AddWithValue("@Id", context.Request["Id"]);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader
(CommandBehavior.SequentialAccess);
if (reader.Read())
{
int bufferSize = 8040;
byte[] chunk = new byte[bufferSize];
long retCount;
long startIndex = 0;
retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);
while (retCount == bufferSize)
{
context.Response.BinaryWrite(chunk);
startIndex += bufferSize;
retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);
}
byte[] actualChunk = new Byte[retCount - 1];
Buffer.BlockCopy(chunk, 0, actualChunk, 0, (int)retCount - 1);
context.Response.BinaryWrite(actualChunk);
}
}
}
public bool IsReusable {
get {
return false;
}
}
}

In web.config file under System.web tag add this line

<httpRuntime maxRequestLength="10240" requestLengthDiskThreshold="100" />

Conclusion:

NOTE : This article is excerpted for the BOOK "ASP.NET 3.5 Unleashed" by Stephen Walther

Here we learned how to use fileupload control to save large files

You have to browse .doc files here

Thanks !!!

For further help feel free to contact me

ignitesofthelp@gmail.com

Interface:

FileUpload Control..JPG


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend