如何上传图片 ftp 服务器 asp.net mvc

How can I upload Image ftp server asp.net mvc

我想使用 ftp 帐户上传图片。我的代码是这样的。但是当我选择图像并提交时,它说我 "Could not find file 'C:\Program Files (x86)\IIS Express\picture.jpg'." 我知道我的图像在我的桌面上,我从那里选择。如果我手动将图像复制到此 IIS 文件夹,它会上传,但这是不明智的。我必须在我想要的地方选择我的形象。但它正在 IIS Express 文件夹中寻找。

      [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();
        }
    }

确保 fileName 中的值是您在这里期望的值:

string fileName = Path.Combine(uploadfile.FileName);

您很可能需要将路径作为字符串以及文件名传递到 Combine 方法中。

string fileName = Path.Combine(varFilePath, uploadfile.FileName);

Path.Combine 需要一个字符串数组来组合:https://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx

我找到了解决我的问题的方法,我想在这里分享,也许一个人可以受益

 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>

控制器动作结果

 [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();
    }

要上传大文件,您可以将此行添加到您的 web.config 感谢Karthik Ganesan

<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />

在 system.web 部分下,默认大小限制为 4 mb