我正在使用mvc 4.5尝试使用FTP上传图像。
我的代码:
public virtual void Send(HttpPostedFileBase uploadfile, string fileName)
{
Stream streamObj = uploadfile.InputStream;
byte[] buffer = new byte[uploadfile.ContentLength];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
string ftpurl = String.Format("{0}/{1}/{2}", _remoteHost, _foldersHost, fileName);
var request = FtpWebRequest.Create(ftpurl) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
request.ContentLength = buffer.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
var response = (FtpWebResponse) request.GetResponse();
if (response != null)
response.Close();
}
代码正在使用localhost,我能够连接到FTP服务器并上传文件,但问题出现在项目发布后。我正在使用SmarterASP来托管该网站。当我尝试在生产中上传图像时,上传方法给了我一个例外,消息显示“无法连接到远程服务器”。
任何线索或解决方法?
异常详情:
System.Net.FtpWebRequest.GetRequestStream()at AgileResto.Controllers.RestaurantsController.Send(HttpPosted FileBase uploadfile,String fileName)at AgileResto.Controllers.RestaurantsController.UploadFile(Int3 2 RestaurantList,HttpPostedFileBase file,HttpPostedFileBase文件2)