C# 如何上载图像ftp服务器asp.net mvc
我想用ftp帐号上传图片。我的代码是这样的。但当我选择image并提交时,它会告诉我“找不到文件‘C:\Program Files(x86)\IIS Express\picture.jpg’。”我知道我的图像在我的桌面上,我从那里选择。如果我将图像手动复制到此IIS文件夹,它将上载,但这是不明智的。我必须在我想要的地方选择我的形象。但它正在IIS Express文件夹中查找C# 如何上载图像ftp服务器asp.net mvc,c#,asp.net-mvc,image,iis,ftp,C#,Asp.net Mvc,Image,Iis,Ftp,我想用ftp帐号上传图片。我的代码是这样的。但当我选择image并提交时,它会告诉我“找不到文件‘C:\Program Files(x86)\IIS Express\picture.jpg’。”我知道我的图像在我的桌面上,我从那里选择。如果我将图像手动复制到此IIS文件夹,它将上载,但这是不明智的。我必须在我想要的地方选择我的形象。但它正在IIS Express文件夹中查找 [HttpPost, ValidateInput(false)] public ActionResul
[HttpPost, ValidateInput(false)]
public ActionResult Insert(Press model, HttpPostedFileBase uploadfile)
{
...........
...........
...........
...........
if (uploadfile.ContentLength > 0)
{
string fileName = Path.Combine(uploadfile.FileName);
var fileInf = new FileInfo(fileName);
var reqFtp =
(FtpWebRequest)
FtpWebRequest.Create(
new Uri("ftp://ftp.adres.com" + fileInf.Name));
reqFtp.Credentials = new NetworkCredential(username, password);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UseBinary = true;
reqFtp.ContentLength = uploadfile.ContentLength;
int bufferlength = 2048;
byte[] buff = new byte[bufferlength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
contentLen = fs.Read(buff, 0, bufferlength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, bufferlength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
}
}
...........
...........
...........
...........
return View();
}
}
确保文件名中的值符合此处的要求:
string fileName = Path.Combine(uploadfile.FileName);
您很可能需要将路径作为字符串以及文件名传递给Combine方法
string fileName = Path.Combine(varFilePath, uploadfile.FileName);
Path.Combine需要一个字符串数组来组合:我找到了解决问题的方法,我想在这里与大家分享,也许一个人可以从中受益
void UploadToFtp(HttpPostedFileBase uploadfile)
{
var uploadurl = "ftp://ftp.adress.com/";
var uploadfilename = uploadfile.FileName;
var username = "ftpusername";
var password = "ftppassword";
Stream streamObj = uploadfile.InputStream;
byte[] buffer = new byte[uploadfile.ContentLength];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
string ftpurl = String.Format("{0}/{1}", uploadurl, uploadfilename);
var requestObj = FtpWebRequest.Create(ftpurl) as FtpWebRequest;
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
requestObj.Credentials = new NetworkCredential(username, password);
Stream requestStream = requestObj.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Flush();
requestStream.Close();
requestObj = null;
}
如何使用Asp.net MVC通过ftp上传文件 查看
<form method="post" enctype="multipart/form-data">
<input type="file" id="postedFile" name="postedFile" />
<input type="submit" value="send" />
</form>
要上载大型文件,可以将此行添加到web.config
多亏了
在system.web部分下,默认为4MB大小限制谢谢您的回复。我想再问一件事。如何获取文件路径。该文件可以是桌面文件或其他文件,HttpPostedFile上的SaveAs方法将允许您向其传递一个字符串以保存客户端上载的文件。
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
//FTP Server URL.
string ftp = "ftp://ftp.YourServer.com/";
//FTP Folder name. Leave blank if you want to upload to root folder.
string ftpFolder = "test/";
byte[] fileBytes = null;
string ftpUserName = "YourUserName";
string ftpPassword = "YourPassword";
//Read the FileName and convert it to Byte array.
string fileName = Path.GetFileName(postedFile.FileName);
using (BinaryReader br = new BinaryReader(postedFile.InputStream))
{
fileBytes = br.ReadBytes(postedFile.ContentLength);
}
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
return View();
}
<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />