Link to home
Create AccountLog in
Avatar of alfardan
alfardanFlag for United Arab Emirates

asked on

Download File

Hi

I have the following link in a page called "MyFiles.aspx":

<a href="Download.aspx?FileName=File1.jpg">Some Text</a>

Open in new window


This (Download.aspx) file contains the following vb.net code in its OnLoad page event:

Response.ContentType = "somefilecontents"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Request.Params("FileName"))
Response.TransmitFile("C:\MyFolder\"+Request.Params("FileName"))
Response.End()

Open in new window


So, when I run my page (MyFiles.aspx) and click on the link, a (File Download) dialog box appears to me and I click on [Save], then when I open that image to view it locally it fails to open and says that the format is NOT supported.

I went and checked if the image can be displayed from the server (using an HTTP request) and it does display perfectly, so why it doesn't display locally when I download it to my machine?
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Is the name of the file actually "file1.jpg"?  I ask because if you save a .gif (or whatever) as a .jpg, the software you're using to view the image may not know how to interpret the file.
try

image/jpeg

Response.ContentType = "somefilecontents"
-->
Response.ContentType = "image/jpeg"
do you really have "somefilecontents" in your code?
set content type appropriately, not just a random unknown thing...

http://msdn.microsoft.com/en-us/library/ms775147.aspx
Avatar of alfardan

ASKER

paulmacd:
Yes, the file name is (file1.jpg)

HainKurt:
Tried that, same issue...
I made this (somefilecontents) as content type in order to trick the http response to force popping up the (Download File) dialog box, because if I didn,'t I thought it might only display the image on a web page. But hoowever, I changed it to its ContentType, and found that it still shows me the (Download File) dialog box, but after I save it locally to my PC and try to open the file, the same issue exists.
How very odd.  I just tested your code and it works great for me.  I even tested several image types (.gif, .jpg, .png) without changing the ContentType and it worked fine each time.  I'm using VS2010 and IE9 on Win7.

How certain are you our images exists at "C:\MyFolder\"?  I ended up using a UNC that pointed to a web server, but it really shouldn't make any difference.  Still, it's worth double-checking.  The file you end up receiving - does it have any data at all in it?  Does it have a file size?
post the file here, so we can have a look at it...
click on download, save the file to local, and post here...
Here you go with an example file I just downloaded but couldn't view.
 file1.jpg

Also, see a screenshot that I attached when I try to open the image that I just downloaded.
User generated image
See, that example image file also fails to display in my previous post too :(
I'm using VS 2008, hold on, let me ask this may be it has something to do with the fact when this failed-to-display-image-file was uploaded ot the server. What Windows/username shall have write access to the server folder in which such files are intended to be uploaded to? I heard it shall be "Network Services". Corrent me if I'm wrong
The image doesn't display because it's not an image - it's an HTML document...maybe with an image inline or something.  In any case, it looks like you're grabbing the wrong file.  I suggest you make sure the .JPG you want to grab is the one you're actually grabbing and that the .JPG you want to grab is actually an image.  

To see the HTML, save the file as a .txt file and open it in a text editor.
paulmacd:

It is an image file, I preview it (itself) before uploading it to the server, it the image displays clearly which tells me it's an IMAGE file.
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you

In my download.aspx file I started with a Header() and Footer(0 subroutines which print the basic HTML tages (such as <html><head> .... <body> ...)

When I removed them then tried to download a file, it just displayed perfectly, thank you very much, this resolved my issue exactly.
Ah!  I guess that explains why it worked for me and not for you.  Good catch.