【问题标题】:How can I upload Image ftp server asp.net mvc如何上传图片 ftp 服务器 asp.net mvc
【发布时间】:2015-03-11 13:33:52
【问题描述】:

我想使用 ftp 帐号上传图片。我的代码是这样的。但是当我选择图像并提交时,它说我“找不到文件 '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();
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc image iis ftp


    【解决方案1】:

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

     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;
        }
    

    【讨论】:

      【解决方案2】:

      确保 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

      【讨论】:

      • 感谢您的回复。我还想问一件事。我怎样才能得到文件路径。该文件可能是桌面或其他地方
      • HttpPostedFile 上的 SaveAs 方法将允许您将字符串传递给它以保存客户端上传的文件。 MSDN documentation
      【解决方案3】:

      如何使用 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 大小限制

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-02
        • 2016-06-24
        • 1970-01-01
        • 2018-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多