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 !




download a file


Posted Date: 31 Jul 2008      Total Responses: 3

Posted By: nirmala       Member Level: Gold     Points: 1



hai friends,
i want to the code for downloading a file,image,doc,pdf etc... in asp.net by using c#.plz send the code in advance.





Responses

Author: Payal Jain    31 Jul 2008Member Level: GoldRating:     Points: 5

i know how to download but my code doesn't promt for destination location....it is fixed...

add this line at the top of ur code file

using System.Net;



and this code for the click event of link button for file

protected void LinkButton1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
client.DownloadFile(@"http://noeld.com/dl.asp?filename=upload.zip&country=USA,", @"G:\Some Usefull software\upload.zip");

}



Author: VijayaShankar    31 Jul 2008Member Level: GoldRating:     Points: 6

The sample code is attached.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<html>
<head>
<title>C#: File download</title>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack){

Response.ContentType="application/ms-word";
Response.AddHeader( "content-disposition","attachment; filename=download.doc");

FileStream sourceFile = new FileStream(@"F:downloadexample.doc", FileMode.Open);
long FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();

Response.BinaryWrite(getContent);
}
}
</script>
</head>

<body>
<form runat="server" id="frmDownload">
<asp:button id="btnDownload" Text = "Click here to download" runat="server" />
</form>
</body>
</html>




Author: Payal Jain    31 Jul 2008Member Level: GoldRating:     Points: 6

try this

private void DownloadFile( string fname, bool forceDownload )
{
string path = MapPath( fname );
string name = Path.GetFileName( path );
string ext = Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;

case ".txt":
type = "text/plain";
break;

case ".doc":
case ".rtf":
type = "Application/msword";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition",
"attachment; filename=" + name );
}
if ( type != "" )
Response.ContentType = type;
Response.WriteFile( path );
Response.End();
}



Post Reply
You must Sign In to post a response.
Next : how to write the code in side the dropdown in asp.net
Previous : Very urgent pls.....- Dropdown list
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use