Uploading a WebImage to FTP

2019-05-31 02:59发布

I am using ASP.Net MVC 4 and WebImage helpers. I need to upload my WebImage object via FTP. I just don't know how to get the WebImage parameters to send to my RequestStream.Write

Here is some code...

        WebImage photo = WebImage.GetImageFromRequest();
        string fileName = System.IO.Path.GetFileName(photo.FileName);

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPaddress +"/images/" + fileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential("****", "*****");
        request.UsePassive = true;

        request.ContentLength = ???????;                      

        byte[] buffer = ????????; //It's not photo.GetBytes;

        Stream requestStream = request.GetRequestStream();              
        requestStream.Write(buffer, 0, request.ContentLength);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        response.Close();

And will I be able to use the photo.Save() to the FTP path afterwards?

1条回答
再贱就再见
2楼-- · 2019-05-31 03:15
byte [] fileContents = photo.GetBytes();
request.ContentLength = fileContents.Length;                       

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

That's how you do it, silly me.

查看更多
登录 后发表回答