1

我正在使用 ASP.Net MVC 4 和 WebImage 助手。我需要通过 FTP 上传我的 WebImage 对象。我只是不知道如何让 WebImage 参数发送到我的 RequestStream.Write

这是一些代码...

        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();

之后我可以使用 photo.Save() 到 FTP 路径吗?

4

1 回答 1

4
byte [] fileContents = photo.GetBytes();
request.ContentLength = fileContents.Length;                       

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

你就是这么干的,我傻。

于 2012-10-08T15:23:14.463 回答