ASP.NET FTP Not Downloading JPEGs Properly...

  • Thread starter Thread starter MU
  • Start date Start date
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?
 
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
 
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
 
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)
 
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))
 
Back
Top