423,867 Members | 3,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

Problem downloading image file

P: n/a
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



--
mo*******@nospam.com
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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();
}
}

"moondaddy" <mo*******@nospam.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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



--
mo*******@nospam.com

Nov 18 '05 #2

P: n/a
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)
--
mo*******@nospam.com
"David W. Simmonds" <da***@simmonds.ca> wrote in message
news:T1bZb.570092$ts4.236844@pd7tw3no...
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();
}
}

"moondaddy" <mo*******@nospam.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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



--
mo*******@nospam.com


Nov 18 '05 #3

P: n/a
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

--
mo*******@nospam.com
"moondaddy" <mo*******@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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)
--
mo*******@nospam.com
"David W. Simmonds" <da***@simmonds.ca> wrote in message
news:T1bZb.570092$ts4.236844@pd7tw3no...
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();
}
}

"moondaddy" <mo*******@nospam.com> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
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



--
mo*******@nospam.com



Nov 18 '05 #4

P: n/a
Hi Moondaddy,
Thanks for your followup. I'm glad that you've resolved your problem and
also thanks alot for sharing your code and experience with us. I'm sure
you'll be proud of yourself.
Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.