使用ASP.NET将多个文件上传到FTP

问题描述 投票:1回答:1

如何在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个回答
0
投票

例如,使用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);
    }
}

基于将文件从字符串或流上传到FTP服务器

© www.soinside.com 2019 - 2024. All rights reserved.