ASP.NET FTP Not Downloading JPEGs Properly...

M

MU

Hello

I have this code and it downloads JPEGS from an FTP site that I am
using. However, when I try to use ASPJPEG or another JPEG component,
it gives me an error saying that the file is corrupt. How do I save
the file as a JPEG?

ASPJPEG Error:
Persits.Jpeg.1 error '80040035'
Not a JPEG file: starts with 0xef 0xbf

ASPThumb Error:
briz.AspThumb error '8000ffff'
Bitmap image is not valid



Here is the code:
myFtpWebRequest = WebRequest.Create("ftp://
ftp.server.com/" & strFileName)
myFtpWebRequest.Credentials = New NetworkCredential
("username", "password")
myFtpWebRequest.Method =
WebRequestMethods.Ftp.DownloadFile
myFtpWebRequest.UseBinary = True

myFtpWebResponse = myFtpWebRequest.GetResponse()

myStreamWriter = New StreamWriter(Server.MapPath("../
images/" & strFileName))
myStreamWriter.Write(New StreamReader
(myFtpWebResponse.GetResponseStream()).ReadToEnd)
myStreamWriter.Close()
myFtpWebResponse.Close()


Thoughts?
 
M

Martin Honnen

MU said:
Hello

I have this code and it downloads JPEGS from an FTP site that I am
using. However, when I try to use ASPJPEG or another JPEG component,
it gives me an error saying that the file is corrupt. How do I save
the file as a JPEG?

ASPJPEG Error:
Persits.Jpeg.1 error '80040035'
Not a JPEG file: starts with 0xef 0xbf

ASPThumb Error:
briz.AspThumb error '8000ffff'
Bitmap image is not valid



Here is the code:
myFtpWebRequest = WebRequest.Create("ftp://
ftp.server.com/" & strFileName)
myFtpWebRequest.Credentials = New NetworkCredential
("username", "password")
myFtpWebRequest.Method =
WebRequestMethods.Ftp.DownloadFile
myFtpWebRequest.UseBinary = True

myFtpWebResponse = myFtpWebRequest.GetResponse()

myStreamWriter = New StreamWriter(Server.MapPath("../
images/" & strFileName))
myStreamWriter.Write(New StreamReader
(myFtpWebResponse.GetResponseStream()).ReadToEnd)
myStreamWriter.Close()
myFtpWebResponse.Close()


Thoughts?

Why are you using a StreamReader and StreamWriter for binary image data?
Simply read chunks of bytes from the response stream and write them to a
file stream.

Or check whether WebClient can do it all for you with a simply method
call:
http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfile.aspx
 
M

MU

Why are you using a StreamReader and StreamWriter for binary image data?
Simply read chunks of bytes from the response stream and write them to a
file stream.

Or check whether WebClient can do it all for you with a simply method
call:http://msdn.microsoft.com/en-us/library/system.net.webclient.download...

I need to FTP to the server to get the Image files. Can I do what you
are suggesting with FTP?
Any hints on examples or how I would accomplish that?

Thanks
 
M

MU

I need to FTP to the server to get the Image files.  Can I do what you
are suggesting with FTP?
Any hints on examples or how I would accomplish that?

Thanks

Here is the solution for anyone looking!

Dim filename As String = "ftp://ftp.server.com/" &
strFileName
Dim ftpReq As FtpWebRequest = WebRequest.Create
(filename)
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = New NetworkCredential("username",
"password")

Dim FTPResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream =
FTPResp.GetResponseStream

Dim img As System.Drawing.Image =
System.Drawing.Image.FromStream(ftpRespStream)
img.Save(Server.MapPath("../images/" & strFileName),
System.Drawing.Imaging.ImageFormat.Jpeg)
 
M

Martin Honnen

MU said:
Here is the solution for anyone looking!

Dim filename As String = "ftp://ftp.server.com/" &
strFileName
Dim ftpReq As FtpWebRequest = WebRequest.Create
(filename)
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = New NetworkCredential("username",
"password")

Dim FTPResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream =
FTPResp.GetResponseStream

Dim img As System.Drawing.Image =
System.Drawing.Image.FromStream(ftpRespStream)
img.Save(Server.MapPath("../images/" & strFileName),
System.Drawing.Imaging.ImageFormat.Jpeg)

WebClient should suffice

Dim wc As New WebClient()
wc.Credentials = New NetworkCredential("username",
"password")
wc.DownloadFile("ftp://ftp.server.com/" &
strFileName, Server.MapPath("../images/" & strFileName))
 

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,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top