473,408 Members | 1,775 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,408 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 2281
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...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.