473,372 Members | 1,049 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,372 software developers and data experts.

Problem downloading image file

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
4 2276
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Dear Sirs, Is there a way to get the width and height of an image without downloading the image, or with just downloading a minimal portion of the image? For instance, I have a list of 10,000...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
4
by: Himanshu | last post by:
hi, Can anybody tell me that thru asp.net using c#, how can we upload and download physical files in any table of SQL Server Database. the uploading part is running successfully but the...
6
by: Dustan | last post by:
Nobody answered last time. I guess they wanted me to give it a shot. Well, here is how I download the image (it's a class method): def download_image(self):...
4
by: aldonnelley | last post by:
Hi there: a bit of a left-field question, I think. I'm writing a program that analyses image files downloaded with a basic crawler, and it's slow, mainly because I only want to analyse files...
2
archulu
by: archulu | last post by:
hai this is archulu, i have a some confusion and doubt in my downloading program.that doubt was in my program i am upload some gif and img file to some path.it's good, after usage of that image i...
14
by: Anthony2oo5 | last post by:
Hey, I'm trying to remotely download an image to my server via the web and an URL that the user inputs. I have managed to do this with a cron job, and write the file to the system no problem. ...
4
by: upendrasingh | last post by:
hi all, i am writing an automation framework in perl that includes web browsing.i am using Win32::IEAutomation package for that purpose.actually the goal is to download a cab file from...
1
by: shahidrasul | last post by:
i want to download a file which user select from gridview, downloading is completing without problem but after download i want to refresh my page because i do some changes in db . but when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.