FileHandler

Vipul
Posted by in ASP.NET category on for Intermediate level | Views : 6828 red flag

You can download and upload word document using this lines of code.


 Download source code for FileHandler

 

 

 
Fille Handler
Follow steps

1.Create one web page,give any name i have given default

2.Write following code in it

Paragraph contents goes here



<asp:Label id="lblFile"

Text="wordDocument"

AssociatedControlID="upFile"

runat="server" />

<asp:FileUpload

id="upFile"

runat="Server" />

<asp:Button

id="btnAdd"

Text="Add Documents"

runat="server" OnClick="btnAdd_Click" />

<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","~/FileHandler.ashx?id={0}")%>'

runat="Server" />

</li>

</ItemTemplate>

<FooterTemplate>

</ul>

</FooterTemplate>

</asp:Repeater>

<asp:SqlDataSource

id="srcFiles"

ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\fileuploadDatabase\App_Data\FilesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

SelectCommand="select id,filename from files"

InsertCommand="insert Files(Filename,FileBytes)values (@fileName,@FileBytes)"

runat="server" ProviderName="System.Data.SqlClient">

<InsertParameters>

<asp:ControlParameter Name="FileName" ControlID="upFile" PropertyName="FileName" />

<asp:ControlParameter Name="FileBytes" ControlID="upFile" PropertyName="FileBytes" />

</InsertParameters>

</asp:SqlDataSource>

3.In Default.aspx.cs

using System.IO;

bool CheckFileType(string fileName)

{

return Path.GetExtension(fileName).ToLower() == ".docx";

}

protected void btnAdd_Click(object sender, EventArgs e)

{

if (upFile.HasFile)

{

if (CheckFileType(upFile.FileName))

srcFiles.Insert();

}

}

4.Take one GenericHandler from Add new item,i have given name is FileHandler.ashx

5.Write following code in it.

<%@ WebHandler Language="C#" Class="FileHandler" %>

using System;

using System.Web;

using System.Data.SqlClient;

using System.Data;

public class FileHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "application/msword";

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=d:\fileuploadDatabase\App_Data\FilesDB.mdf;Integrated Security=True;User Instance=True");

SqlCommand cmd = new SqlCommand("select filebytes from files where id=@id", con);

cmd.Parameters.AddWithValue("@id", context.Request["id"]);

using (con)

{

con.Open();

byte[] file = (byte[])cmd.ExecuteScalar();

context.Response.BinaryWrite(file);

}

}

public bool IsReusable {

get {

return false;

}

}

}

6.This .docx is for word 2007 .

7.If you have not 2007 ms word than check you extenstion of any word file and replace .docx to .doc

8.Table structure

Table Name:Files

id int not null

FileName nvarchar(50) not null

FileBytes varbinary(max)

Page copy protected against web site content infringement by Copyscape

About the Author

Vipul
Full Name: vipul vk
Member Level: Starter
Member Status: Member
Member Since: 4/15/2009 9:08:15 PM
Country:

http://www.dotnetfunda.com
Working as a software developer

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)