We help IT Professionals succeed at work.

upload image through ftpwebrequest

StewSupport
StewSupport asked
on
i'm trying to upload a file from c:\temp\test.jpg to my web server. however, i can't seem to get the file up there correctly. my ftp connection is fine it just the filestream function that is not working right. my file keeps getting 0kb. If i use the streamwriter, it will upload with the right size for the file but the image doesn't display when you go to it. any idea? thanks
Dim myFtpWebRequest As FtpWebRequest
        Dim myFtpWebResponse As FtpWebResponse
        Dim myStreamWriter As StreamWriter
        Dim fs As FileStream = Nothing
        Dim data(4096) As Byte ' default ftp buffer size
        Dim bytesRead As Integer = 0

        ServicePointManager.ServerCertificateValidationCallback = Function(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
        Try
            myFtpWebRequest = WebRequest.Create("ftp://www.yahoo.com/" & filename)
            myFtpWebRequest.Timeout = -1
            myFtpWebRequest.Credentials = New Net.NetworkCredential("test", "test")
            myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
            myFtpWebRequest.EnableSsl = True
            myFtpWebRequest.UseBinary = True
            'myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
            'myStreamWriter.Write(New StreamReader("c:\temp\" & filename).BaseStream)
            fs = New FileStream("c:\temp\" & filename, FileMode.Create, FileAccess.Write, FileShare.Read)
            Do
                bytesRead = myFtpWebRequest.GetResponse.GetResponseStream.Read(data, 0, data.Length)
                fs.Write(data, 0, bytesRead)
            Loop Until bytesRead = 0
            If fs IsNot Nothing Then
                fs.Close()
                fs = Nothing
            End If

            myStreamWriter.Close()


            myFtpWebResponse = myFtpWebRequest.GetResponse()
            myFtpWebResponse.Close()
        Catch ex As Exception

        End Try

Open in new window

Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
>i'm trying to upload a file

The code is doing the opposite. Its reading bytes from FTP response and writing to the C: file.

Author

Commented:
it works ok for streamwriter though just not with jpg. if i do WebRequest.Create("c:\temp\" & filename) then it won't do it because there's no system.web.FtpWebRequest, it will try to use filerequest instead.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Give following code a try


I hope yahoo URL is dummy and not actual URL


Dim myFtpWebRequest As FtpWebRequest
ServicePointManager.ServerCertificateValidationCallback = Function(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
Try
  myFtpWebRequest = WebRequest.Create("ftp://www.yahoo.com/" & filename)
  myFtpWebRequest.Timeout = -1
  myFtpWebRequest.Credentials = New Net.NetworkCredential("test", "test")
  myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
  myFtpWebRequest.EnableSsl = True
  myFtpWebRequest.UseBinary = True
  Dim FileBytes As Byte() = IO.File.ReadAllBytes("C:\temp\test.jpg")
  myFtpWebRequest.GetResponse.GetResponseStream.Write(FileBytes, 0, FileBytes.Length)
  myFtpWebRequest.GetResponse().Close()
Catch ex As Exception

End Try

Open in new window

Author

Commented:
i get a stream does not support writing error.

Author

Commented:
this solution works
Hello,
This code is helpful for uploading file using ftp

public string uploadFileUsingFTP(byte[] fileStream,
CashmanUpload.CashmanUploadService.EventImageVaria ble eventVariable)
{
// get the ftppath from config file and the image loctaion
//from input parameters
string CompleteFTPPath = eventVariable.UploadImagepath +
eventVariable.ImageName;
// set the ftp userid
string UName =
Convert.ToString(eventVariable.refFTPUserID);
// set the ftp password
string PWD =
Convert.ToString(eventVariable.refFTPPassword);
string retrunvalue = "";
CashmanUpload.CashmanUploadService.UploadService
uploadService =
new CashmanUpload.CashmanUploadService.UploadService() ;

try
{
if (!
uploadService.CheckImageExitsservice(eventVariable ))
{
//Create a FTP Request Object and Specfiy a
Complete Path
FtpWebRequest reqObj =
(FtpWebRequest)FtpWebRequest.Create(new Uri(CompleteFTPPath));
reqObj.KeepAlive = false;
// set the maximum timeout for upload the heavy
images
reqObj.Timeout = 10000000;
// set the maximum write timeout for upload the
heavy images
reqObj.ReadWriteTimeout = 10000000;
//If you want to access Resourse Protected You
need to give User Name and PWD
reqObj.Credentials = new NetworkCredential(UName,
PWD);
//Call A FileUpload Method of FTP Request
Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
// Copy the contents of the file to the request
stream.
reqObj.ContentLength = fileStream.Length;
// create the Stream object to write the file on
FTP
Stream requestStream = reqObj.GetRequestStream();
requestStream.Write(fileStream, 0,
fileStream.Length);
requestStream.Close();
// get the final response
FtpWebResponse response =
(FtpWebResponse)reqObj.GetResponse();
response.Close();
// if sucessfull the reurn OK
retrunvalue = "OK";
}
else
{
// else retrun NOTDONE to retry the same image
again
retrunvalue = "NOTDONE";
}
}
catch (Exception ex)
{
UserExceptionLog.CreateExceptionLog(ex);
//MessageBox.Show(ex.Message + " function name is
uploadFileUsingFTP 731");
}
return retrunvalue;
}