【问题标题】:Upload multiple files to FTP with ASP.NET使用 ASP.NET 将多个文件上传到 FTP
【发布时间】:2019-12-03 15:14:56
【问题描述】:

ASP.NET MVC中如何将多个文件上传到FTP?

与组件或其他...

string path = Server.MapPath("~/Image/");

HttpFileCollectionBase MyFileCollection = Request.Files;
HttpPostedFileBase MyFile;
int i ;
int j = MyFileCollection.Count;
int FileLen;
Stream MyStream;

for(i = 0; i < j; i++)
{
    MyFile = MyFileCollection[i];

    FileLen = MyFile.ContentLength;
    byte[] input = new byte[FileLen];

    MyStream = MyFile.InputStream;

    MyStream.Read(input, 0, FileLen);

    for (int Loop = 0; Loop < FileLen; Loop++)
    {
        MyString = path + input[Loop].ToString();
    }
    MyFileCollection[i].SaveAs(MyString + ".jpg");
}

【问题讨论】:

    标签: c# .net asp.net-mvc ftp upload


    【解决方案1】:

    例如使用FtpWebRequest.GetRequestStream:

    foreach (HttpPostedFileBase file in Request.Files)
    {
        string url = "ftp://example.com/" + Path.GetFileName(file.FileName);
        FtpWebRequest request = FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential("username", "password");
    
        using (Stream requestStream = request.GetRequestStream())
        {
            file.InputStream.CopyTo(requestStream);
        }
    }
    

    基于Upload a file to an FTP server from a string or stream

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-13
      • 2011-08-25
      • 1970-01-01
      • 2013-04-26
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多