Problem downloading image file

M

moondaddy

Using vb.net I need to download image files to the client browser where they
can save to disk. Below is some sample code I'm using. when I run this the
File Download window in the browser says:

File name: ViewAttachment
File type:
From localhost

1) "ViewAttachment" is the name of the aspx page and not the image file.
2) Its not picking up the file type of jpg. For ContentType I used
image/pjpeg becuase thats what i say when I ran some sample code to upload
the picture but I dont know for sure if thats correct for this code snippet.
3) when I open the file its all mumbo jumbo numbers.

Where am I going wrong?





Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim path As String = Server.MapPath(".") & "\images\test\Winter.jpg"
Dim fs As New FileStream(path, FileMode.Open)
Dim byteRead As Integer

Response.ClearHeaders()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment;filename=" & path)
Response.ContentType = "image/pjpeg"
'Response.WriteFile(path) 'NOTE: If I use this line I get an error
saying that the file is already in use.

byteRead = fs.ReadByte()

While byteRead <> -1
Response.Write(byteRead)
byteRead = fs.ReadByte()
End While

fs.Close()

End Sub
 
D

David W. Simmonds

Here is what I do. The code is in C#, but that should not be too much of a
problem.

I have a form that has an Image object on it and a button named Save. The
form is passed a parameter that is the url of the image. The image will show
up in the form as the full high-res image. The user could just rclick and
hit Save, but a Save button is more obvious.

public class ImageViewer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SaveButton;
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
Image1.ImageUrl = Request["filename"];
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SaveButton.Click += new
System.EventHandler(this.SaveButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void SaveButton_Click(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = "Image/JPG";
string name = Image1.ImageUrl;
int n = Image1.ImageUrl.LastIndexOf("/");
name = Image1.ImageUrl.Substring(n+1);

Response.AddHeader("Content-Disposition","attachment;filename=\""+name+"\"")
;
Response.WriteFile(Image1.ImageUrl);
Response.End();
}
}
 
M

moondaddy

Thanks that got me a step closer. Now I can download and save the image to
the client machine and when I open it, its a viewable image (as in not
corrupt). My only remaining issue is that when the Open or Save window
opens up, the default file name isnt the name of the image but is defaulting
to the name of the aspx page like this:

File name: ViewAttachment
File type:
From localhost

How can I set it to the correct file name?

Here's my revised code:

Dim path As String = Server.MapPath(".") & "\images\test\Winter.jpg"
Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.AddHeader("Content-Disposition",
"attachment;filename=\Winter.jpg\")
Response.ContentType = "image/jpg"
Response.WriteFile(path)


--
(e-mail address removed)
David W. Simmonds said:
Here is what I do. The code is in C#, but that should not be too much of a
problem.

I have a form that has an Image object on it and a button named Save. The
form is passed a parameter that is the url of the image. The image will show
up in the form as the full high-res image. The user could just rclick and
hit Save, but a Save button is more obvious.

public class ImageViewer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SaveButton;
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
Image1.ImageUrl = Request["filename"];
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SaveButton.Click += new
System.EventHandler(this.SaveButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void SaveButton_Click(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = "Image/JPG";
string name = Image1.ImageUrl;
int n = Image1.ImageUrl.LastIndexOf("/");
name = Image1.ImageUrl.Substring(n+1);

Response.AddHeader("Content-Disposition","attachment;filename=\""+name+"\"")
;
Response.WriteFile(Image1.ImageUrl);
Response.End();
}
}
 
M

moondaddy

I found the problem

I had to change the evil line from:

Response.AddHeader("Content-Disposition",
"attachment;filename=\Winter.jpg\")
to
Response.AddHeader("Content-Disposition", "attachment;filename=Winter.jpg")

and now it works. For anyone who wants to know, there's the complete vb
code behind that works good.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim path As String = Server.MapPath(".") & "\images\test\Winter.jpg"
Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.AddHeader("Content-Disposition",
"attachment;filename=Winter.jpg")
Response.ContentType = "image/jpg"
Response.WriteFile(path)

End Sub





--
(e-mail address removed)
moondaddy said:
Thanks that got me a step closer. Now I can download and save the image to
the client machine and when I open it, its a viewable image (as in not
corrupt). My only remaining issue is that when the Open or Save window
opens up, the default file name isnt the name of the image but is defaulting
to the name of the aspx page like this:

File name: ViewAttachment
File type:
From localhost

How can I set it to the correct file name?

Here's my revised code:

Dim path As String = Server.MapPath(".") & "\images\test\Winter.jpg"
Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.AddHeader("Content-Disposition",
"attachment;filename=\Winter.jpg\")
Response.ContentType = "image/jpg"
Response.WriteFile(path)


--
(e-mail address removed)
David W. Simmonds said:
Here is what I do. The code is in C#, but that should not be too much of a
problem.

I have a form that has an Image object on it and a button named Save. The
form is passed a parameter that is the url of the image. The image will show
up in the form as the full high-res image. The user could just rclick and
hit Save, but a Save button is more obvious.

public class ImageViewer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SaveButton;
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
Image1.ImageUrl = Request["filename"];
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.SaveButton.Click += new
System.EventHandler(this.SaveButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
private void SaveButton_Click(object sender, System.EventArgs e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = "Image/JPG";
string name = Image1.ImageUrl;
int n = Image1.ImageUrl.LastIndexOf("/");
name = Image1.ImageUrl.Substring(n+1);
Response.AddHeader("Content-Disposition","attachment;filename=\""+name+"\"")
 

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,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top