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


Top 5 Contributors of the Month
david stephan

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: February 02, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

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" %>




FileUpload Large




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


 Subscribe to Articles

     

Further Readings:

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    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend