本文介绍了上传WebImage到FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用ASP.Net MVC 4和WebImage帮手。我需要通过FTP上传我的WebImage对象。我只是不知道如何让WebImage参数发送给我的RequestStream.Write
下面是一些代码...
WebImage照片= WebImage.GetImageFromRequest();
字符串文件名= System.IO.Path.GetFileName(photo.FileName);
的FtpWebRequest请求=(的FtpWebRequest)WebRequest.Create(FTPaddress +/图片/+文件名);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials =新的NetworkCredential(****,*****);
request.UsePassive = TRUE;
request.ContentLength = ???????;
字节[]缓冲= ????????; //这不是photo.GetBytes;
流requestStream = request.GetRequestStream();
requestStream.Write(缓冲液,0,request.ContentLength);
requestStream.Close();
FtpWebResponse响应=(FtpWebResponse)request.GetResponse();
response.Close();
和我将能够在事后利用photo.Save()以FTP路径?
解决方案
字节[] = fileContents photo.GetBytes();
request.ContentLength = fileContents.Length;
流requestStream = request.GetRequestStream();
requestStream.Write(fileContents,0,fileContents.Length);
requestStream.Close();
这是你如何做到这一点,我傻。
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?
解决方案
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.
这篇关于上传WebImage到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!