C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Uploading and Downloading files


Posted Date: 21 May 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: vijethaMember Level: Gold    
Rating: Points: 10




SqlConnection sqlConn = new SqlConnection("connection string");

Add the following code in html page

<form id="form1" runat="server">
<table style="z-index: 103; left: 94px; position: absolute; top: 71px">
<tr>
<td>
<input id="txtFileContents" type="file" runat="server" name="FileUpload"/>
</td>
</tr>
<tr>
<td align="right">
<asp:button id="btnUpload" Text="Upload" Runat="server" OnClick="btnUpload_Click" ></asp:button>
</td>
</tr>
</table>


<asp:GridView ID="gvFiles" runat="server" Style="z-index: 104; left: 90px; position: absolute;
top: 150px" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="gvFiles_SelectedIndexChanged">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate >
<asp:LinkButton ID="lnkDownload" ForeColor="DarkOrange" runat="server" CausesValidation="False" CommandName="Select"
Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>


</form>


Add the following code in code file
------------------------------------

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Fill();
}
}


protected void btnUpload_Click(object sender, EventArgs e)
{
string strFileName = txtFileContents.PostedFile.FileName.Substring(txtFileContents.PostedFile.FileName.LastIndexOf("\\") + 1);
string strFileType = txtFileContents.PostedFile.ContentType;
int intFileLen = txtFileContents.PostedFile.ContentLength;
Stream objStream = txtFileContents.PostedFile.InputStream;
byte[] bytFile = new byte[intFileLen];
objStream.Read(bytFile, 0, intFileLen);

SqlCommand cmdUploadFile = new SqlCommand("sp_Files", sqlConn);
cmdUploadFile.CommandType = CommandType.StoredProcedure;
cmdUploadFile.Parameters.Add("@FileName ", SqlDbType.VarChar, 50);
cmdUploadFile.Parameters.Add("@Files", SqlDbType.Image);
cmdUploadFile.Parameters.Add("@FileType", SqlDbType.VarChar, 50);
cmdUploadFile.Parameters[0].Value = strFileName;
cmdUploadFile.Parameters[1].Value = bytFile;
cmdUploadFile.Parameters[2].Value = strFileType;

sqlConn.Open();
cmdUploadFile.ExecuteNonQuery();
sqlConn.Close();
Fill();
}


protected void gvFiles_SelectedIndexChanged(object sender, EventArgs e)
{
string strId = gvFiles.SelectedRow.Cells[1].Text;
SqlCommand cmd = new SqlCommand("select FileType,Files,FileName from Files where id=" + strId + "", sqlConn);
sqlConn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string strFileType = dr[0].ToString();
string strFileName = dr[2].ToString();
//if below statement is not there, it will not effect the operation
Response.ContentType = strFileType;
Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + "");
Response.BinaryWrite((byte[])dr[1]);
Response.End();

}
sqlConn.Close();
}


public void Fill()
{
SqlDataAdapter sdaFiles = new SqlDataAdapter("select id,FileType,Files,FileName from Files", sqlConn);
DataSet dsFiles = new DataSet();
sdaFiles.Fill(dsFiles);
gvFiles.DataSource = dsFiles;
gvFiles.DataBind();
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to show images in GridView
Previous Resource: Data gridview with images
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use