C# / ASP.Net code to allow web page visitors to download files from web server

R

r dubey

Hi, I'm using webclient to enable download of files.
However, files get copied to the server rather than the
client machine. What could be the problem.

Here is the sample that I've used:

WebClient myWebClient = new WebClient();
myWebClient.DownloadFile
("http://www.myweb.com/testfile.txt" ,"c:/test.txt");

The problem is that the test.txt gets copied to the C:\
drive of the server rather than the client machine.
 
M

Mark Fitzpatrick

Are you running this in an ASP.Net page? Then of course it's going to
download to the server because the code will only run on the server. To get
this to work like you want you would need to be executing this as a program
on the client computer.

You can always use a response.writefile to the browser. You can't control
where it's saved on the user's machine, and basically it will work exactly
the same as if you made a link to the file (ie: text files will probably be
opened in the browser since the browser knows what to do with them, try
zipping the file so user's are prompted for download).

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Ê

ʹÃûÑï

try this one^_^¡£
1.get file from dataserver ,and save it into web server'harddisk
2.client get file from web server

private void BT_DownFile_Click(object sender, System.EventArgs e)
{
string str="SELECT CONTENT,FILE_NAME FROM FILES WHERE STS='A' AND
FILE_ID="+Request.QueryString ["FILE_ID"].ToString();
OleDbCommand myCommand=new OleDbCommand (str,myConnection);

if(myConnection.State.ToString().ToUpper()!="OPEN")
myConnection.Open();

OleDbDataReader myReader=myCommand.ExecuteReader();
if (myReader.Read())
{
string filename=myReader.GetString(1);
byte[] FileData = (byte[])myReader["CONTENT"];//ÀàÐÍת»¯
string
filedirect=ConfigurationSettings.AppSettings["TmpDirForDownLoad"].ToString()
+"\\"+Request.QueryString ["FILE_ID"].ToString();

DirectoryInfo DirInfo=new DirectoryInfo(filedirect);//´´Ä¿Â¼
if (!DirInfo.Exists)
Directory.CreateDirectory(filedirect);

string fullname=filedirect+"\\"+filename;//ºÏÈ«²¿ÎļþÃû
if(!System.IO.File.Exists(fullname))//Èç¹ûÎļþ²»´æÔÚÔÙдÎļþ,·ñÔò²»ÖØд
ÁË
{
FileStream fs=System.IO.File.Create(fullname);//дÎļþ
fs.Write(FileData,0,FileData.Length);
fs.Close();
}

System.IO.FileInfo fi=new System.IO.FileInfo(fullname);
Response.Clear();

Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.
UrlEncode(fi.Name));
Response.AddHeader("Content-Length",fi.Length.ToString()) ;
Response.ContentType="application/octet-stream" ;
Response.WriteFile(fi.FullName);
Response.End();
}
myReader.Close();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top