Uploading a WebImage to FTP

妖精的绣舞 提交于 2019-12-08 04:29:54

问题


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:


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.



来源:https://stackoverflow.com/questions/12784550/uploading-a-webimage-to-ftp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!