473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

WebClient.DownloadFile only see's Local????

Why does WebClient.DownloadFile loose my completed path?

Ok, I have a function in my app then when my button is clicked it checks to
see if the files come from a local drive or a http address. The portion of
the code that pulls from the drive works great, but when the http address is
active it changes the originating path to my C:\Windows\System32 folder
instead of the proper path.

I keep the path as a field on my form so I am certain it is correct.

This is my onclick function!

string fileName = BoxImageName.Text;
string filePath = BoxImagePath.Text;

if(Configuration.imageDirType == "Drive")
{
FileInfo MyFileInfo;
long StartPos = 0, FileSize;
MyFileInfo = new FileInfo(filePath);
FileSize = MyFileInfo.Length;

Response.ContentType = "application/x-msdownload";
Response.AppendHeader( "content-disposition","attachment; filename=" +
fileName);
Response.WriteFile(filePath, StartPos, FileSize);
Response.End();
}
else if(Configuration.imageDirType == "WWW")
{
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(filePath,fileName);
}

The BoxImagePath.Text does have the right information in it, when you copy
it's contents to a browser window it displays perfectly, so the path is
correct.

What else could I be missing??
Is there a better way I should be doing this??
Is there a way to force this download to prompt for a saveas location like
the x-msdownload does??

Thanks all!
--
D @ premierdata
Nov 19 '05 #1
8 8982
Hi Dewright,

Welcome to ASP.NET newsgroup.
From your description and the code snippet, you're performing a file
downloading task and the source file may come from local disk or on a
remote server and exposed through http url address. Also ,when the file is
on remote address, you'll use Webclient.DownloadFile to retrieve it to
local disk. However, you found that when calling the WebClient.DownLoad
file, it'll always save the file under the "C:\Windows\System32" folder
rather than your current app's folder, yes?

Based on my understanding, the problem you met is likely caused by the
Default "Current Directory" value of the ASP.NET worker process. Is the
"fileName" you specified in

myWebClient.DownloadFile(filePath,fileName);

a relative path (or just a file name without full path), if so the runtime
will assume that it's in the current directory and concate it with the
current directory value. And for windows process, sub process will inherit
the current directory value from it's parent process(creator process). And
ASP.NET's worker process is created at the first time a certain asp.net
page being requested by the IIS's service process, so it'll inherit the IIS
process's current dir value which is

"c:\windows\system32\inetsrv"

You can call "System.IO.Directory.GetCurrentDirectory()" to verify this.
Also, if you want to change the current dir value, we can use the

System.IO.Directory.SetCurrentDirectory(Server.Map Path("~/"));

to assign the application's root dir as the current dir. So I think you
can try adjusting the current dir value before calling webclient.DownLoad
file to see whether it helps.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 19 '05 #2
Thanks Steven,

I am passing to the command a known URL, in a full
HTTP://SERVERNAME/Foldername/Subfolder/filename.ext format. I can verify that
the path is good as I can take the URL and send it to another window and have
it show.

so in all cases I am sending to the app;
myWebClient.DownloadFile("http://tgrams/imageviewer/mt/notes/20/ne/12/mt20300202.tif","mt20300202.tif")
as the actual tool. But the actual path is dropped out.

Thoughts?
--
D @ premierdata
"Steven Cheng[MSFT]" wrote:
Hi Dewright,

Welcome to ASP.NET newsgroup.
From your description and the code snippet, you're performing a file
downloading task and the source file may come from local disk or on a
remote server and exposed through http url address. Also ,when the file is
on remote address, you'll use Webclient.DownloadFile to retrieve it to
local disk. However, you found that when calling the WebClient.DownLoad
file, it'll always save the file under the "C:\Windows\System32" folder
rather than your current app's folder, yes?

Based on my understanding, the problem you met is likely caused by the
Default "Current Directory" value of the ASP.NET worker process. Is the
"fileName" you specified in

myWebClient.DownloadFile(filePath,fileName);

a relative path (or just a file name without full path), if so the runtime
will assume that it's in the current directory and concate it with the
current directory value. And for windows process, sub process will inherit
the current directory value from it's parent process(creator process). And
ASP.NET's worker process is created at the first time a certain asp.net
page being requested by the IIS's service process, so it'll inherit the IIS
process's current dir value which is

"c:\windows\system32\inetsrv"

You can call "System.IO.Directory.GetCurrentDirectory()" to verify this.
Also, if you want to change the current dir value, we can use the

System.IO.Directory.SetCurrentDirectory(Server.Map Path("~/"));

to assign the application's root dir as the current dir. So I think you
can try adjusting the current dir value before calling webclient.DownLoad
file to see whether it helps.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #3
Hi Dewright,

Thanks for your followup.
As you mentioned that you using the following statement to call
WebClient.DownLoadFile:

myWebClient.DownloadFile("http://tgrams/imageviewer/mt/notes/20/ne/12/mt2030
0202.tif","mt20300202.tif")

That means you'll request the file at

http://tgrams/imageviewer/mt/notes/2...mt20300202.tif

and save it on your local machine as "mt20300202.tif". In fact, the
WebClient.DownLoadFile will perform the following steps internally:

1. Use HttpWebRequest to request the file through the url you
specified(http://tgrams/imageviewer/mt/notes/2...mt20300202.tif)

2. Then, read the responseStream from the above httpWebRequest and create a
FileStream through the local path you specified (the second param in the
DownLoadFile method). Also, since you specify a relative path, runtime will
append it with the current directory's path( for ASP.NET, it is
"c:\windows\system32\inetsrv"). So if the DownloadFile function correctly,
we can find that the file is downloaded and saved at

"c:\windows\system32\inetsrv\mt20300202.tif", is this what you got
currently? If so, this is the correct and expected behavior. If not ,
would you try using "WebClient.DownLoadData" to see whether it can
correctly retrieve the file stream from the remote url. If DownloadData
function correctly, I still think the problem is with your local file path.

Please feel free to post here if there're anything unclear. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #4
Hi Steven;

Ok, I did go through and try your suggestion, I used this to get a buffer of
the file,

WebClient fileClient = new WebClient();
fileClient.Headers.Add("Content-Type: application/x-msdownload");
byte [] myDataBuffer = fileClient.DownloadData(filePath);
string download = Encoding.ASCII.GetString(myDataBuffer);
Response.Write(download);

When I run this I get a stream of text to my screen that does show the file
contents, based on the Multi-Page TIF V2 header in the text.

Alas if I turn the Response.Write to a WriteFile it gives the same exception
as below.

This doesn't make much sense to me, as I am able to get the stream to
display but if I try to write it to a file it chokes again.

Thoughts?

--
D @ premierdata
"Steven Cheng[MSFT]" wrote:
Hi Dewright,

Thanks for your followup.
As you mentioned that you using the following statement to call
WebClient.DownLoadFile:

myWebClient.DownloadFile("http://tgrams/imageviewer/mt/notes/20/ne/12/mt2030
0202.tif","mt20300202.tif")

That means you'll request the file at

http://tgrams/imageviewer/mt/notes/2...mt20300202.tif

and save it on your local machine as "mt20300202.tif". In fact, the
WebClient.DownLoadFile will perform the following steps internally:

1. Use HttpWebRequest to request the file through the url you
specified(http://tgrams/imageviewer/mt/notes/2...mt20300202.tif)

2. Then, read the responseStream from the above httpWebRequest and create a
FileStream through the local path you specified (the second param in the
DownLoadFile method). Also, since you specify a relative path, runtime will
append it with the current directory's path( for ASP.NET, it is
"c:\windows\system32\inetsrv"). So if the DownloadFile function correctly,
we can find that the file is downloaded and saved at

"c:\windows\system32\inetsrv\mt20300202.tif", is this what you got
currently? If so, this is the correct and expected behavior. If not ,
would you try using "WebClient.DownLoadData" to see whether it can
correctly retrieve the file stream from the remote url. If DownloadData
function correctly, I still think the problem is with your local file path.

Please feel free to post here if there're anything unclear. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #5
Hi Dewright,

Thanks for your response. So I think the code worked well in your web app,
I think there maybe some misunderstanding on the WebClient.DownLoadFile
method. This method will only retrieve the filestream from the remote url
then save it to a
"LOCAL FILE" on the machine (where your code executing). So if you want to
write it down to your asp.net app's client, we must use Response.WriteFile
and provide the filepath where the "DownLoadFile" saved that remote stream
to write it to our web application's client user. So the typical code will
be something like:

=======================
string url = "http://remoteserver/folder/file.ext";
WebClient wc = null;

string path = "~/temp/";
string filename = DateTime.Now.Ticks + ".tmp";

try
{
wc = new WebClient();

wc.DownloadFile(url, Server.MapPath(path + filename));

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("ContentType","application/octet-stream");
Response.AddHeader("Content-Disposition", "attachment;
filename=aspnet.html");

Response.WriteFile(Server.MapPath(path+filename));

Response.End();

}
catch(System.Threading.ThreadAbortException taex)
{
//do nothing just to bypass the ThreadAbortException
}
catch(WebException webex)
{
Response.Write("<br>" + webex.ToString());
}
catch(Exception ex)
{
Response.Write("<br>" + ex.ToString());
}
=========================

In the above code snippet, I first use WebClient.DownLoadFile to save the
file in my server's "temp" folder which is under my applciation's root dir.
Then use Response.WriteFile to write it out.

So we can see that using Webclient.Download file is not a good approach in
such scenario, we can directly use DownLoadData to retrieve the binary
stream and then write it into Respons.OutputStream directly which won't
need the additional temp file on our server machine. How do you think ?

If there're anything unclear, please feel free to let me know. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #6
Ok, I see what you are going.

Is it possible to do a WebClient.DownloadFile to the user if the file is
coming from my own virtual directory? I mapped the folder to my approot as a
scannedimages folder that I can now access from my site.

Thoughts?
--
D @ premierdata
"Steven Cheng[MSFT]" wrote:
Hi Dewright,

Thanks for your response. So I think the code worked well in your web app,
I think there maybe some misunderstanding on the WebClient.DownLoadFile
method. This method will only retrieve the filestream from the remote url
then save it to a
"LOCAL FILE" on the machine (where your code executing). So if you want to
write it down to your asp.net app's client, we must use Response.WriteFile
and provide the filepath where the "DownLoadFile" saved that remote stream
to write it to our web application's client user. So the typical code will
be something like:

=======================
string url = "http://remoteserver/folder/file.ext";
WebClient wc = null;

string path = "~/temp/";
string filename = DateTime.Now.Ticks + ".tmp";

try
{
wc = new WebClient();

wc.DownloadFile(url, Server.MapPath(path + filename));

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("ContentType","application/octet-stream");
Response.AddHeader("Content-Disposition", "attachment;
filename=aspnet.html");

Response.WriteFile(Server.MapPath(path+filename));

Response.End();

}
catch(System.Threading.ThreadAbortException taex)
{
//do nothing just to bypass the ThreadAbortException
}
catch(WebException webex)
{
Response.Write("<br>" + webex.ToString());
}
catch(Exception ex)
{
Response.Write("<br>" + ex.ToString());
}
=========================

In the above code snippet, I first use WebClient.DownLoadFile to save the
file in my server's "temp" folder which is under my applciation's root dir.
Then use Response.WriteFile to write it out.

So we can see that using Webclient.Download file is not a good approach in
such scenario, we can directly use DownLoadData to retrieve the binary
stream and then write it into Respons.OutputStream directly which won't
need the additional temp file on our server machine. How do you think ?

If there're anything unclear, please feel free to let me know. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #7
This is great, I adapted what you posted to my files and it is working great.
So I am now just putting into place a function to delete the tmp file after
the response.end.

Thanks for your help with the logic!
--
D @ premierdata
"Steven Cheng[MSFT]" wrote:
Hi Dewright,

Thanks for your response. So I think the code worked well in your web app,
I think there maybe some misunderstanding on the WebClient.DownLoadFile
method. This method will only retrieve the filestream from the remote url
then save it to a
"LOCAL FILE" on the machine (where your code executing). So if you want to
write it down to your asp.net app's client, we must use Response.WriteFile
and provide the filepath where the "DownLoadFile" saved that remote stream
to write it to our web application's client user. So the typical code will
be something like:

=======================
string url = "http://remoteserver/folder/file.ext";
WebClient wc = null;

string path = "~/temp/";
string filename = DateTime.Now.Ticks + ".tmp";

try
{
wc = new WebClient();

wc.DownloadFile(url, Server.MapPath(path + filename));

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("ContentType","application/octet-stream");
Response.AddHeader("Content-Disposition", "attachment;
filename=aspnet.html");

Response.WriteFile(Server.MapPath(path+filename));

Response.End();

}
catch(System.Threading.ThreadAbortException taex)
{
//do nothing just to bypass the ThreadAbortException
}
catch(WebException webex)
{
Response.Write("<br>" + webex.ToString());
}
catch(Exception ex)
{
Response.Write("<br>" + ex.ToString());
}
=========================

In the above code snippet, I first use WebClient.DownLoadFile to save the
file in my server's "temp" folder which is under my applciation's root dir.
Then use Response.WriteFile to write it out.

So we can see that using Webclient.Download file is not a good approach in
such scenario, we can directly use DownLoadData to retrieve the binary
stream and then write it into Respons.OutputStream directly which won't
need the additional temp file on our server machine. How do you think ?

If there're anything unclear, please feel free to let me know. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #8
You're welcome Dewright,

Also, I think you can also consider using the WebClient.DownloadData which
return the byte[] stream of the remote data, thus we can directly write
that byte stream into our response's output buffer (no temp file needed).
Anyway, you can choose the one you feel most convenient.

Thanks again for your posting.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James Write | last post by:
Hi, I am using the webclient.downloadfile() method to download a file from a remote web server to the local harddrive. public CopyFile(string sSrcPath, string sDestPath) { WebClient oClient...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
1
by: Cheri Elkin | last post by:
I recieve the following error when attempting to download a file from our web server to be placed on our hard drive. Any clues as to why I would get this error? By the way, the directory already...
1
by: peter.rietmann | last post by:
I have been suddenly having a performance problem downloading images from one server (productive) to another (productive) using the following code .. WebClient webClient = new WebClient(); try...
2
by: Lila Godel | last post by:
I am having a problem with the download of web pages via the WebClient.DownloadFile function in the specialized VB.Net 2003 I.E. plug-in I am designing to speed up the work on my latest project. ...
1
by: David Satz | last post by:
Hello--I just upgraded to Visual Studio .NET 2005 and suddenly, all my .NET 1.1 applications that accessed Web sites have broken. For example, this code: WebClient wc = new WebClient();...
1
by: vin.dor | last post by:
Dear All, I am using WebClient in my Visual Studio .Net 2003 project to download an image from the Internet. The following is my function: C# Code: public static bool downloadFile(string...
1
by: Mike | last post by:
I am using PowerShell to download an XML file into a string using the DownloadString(URI) method of System.Net.WebClient object. Sample commands below: $Result = $Null; $WebClient = new-object...
8
by: =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post by:
WebClient.DownloadFile I am using the WebClient.DownloadFile function to download a file from server to a local client. When I execute the below code, file is created in server and not in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.