上传到ftp服务器的文件已损坏c#

时间:2016-11-17 17:18:24

标签: c#

我不确定为什么我的文件已损坏。我没有使用StreamReader,这是大多数人常见的问题。它上传成功我似乎无法找到文件被破坏的问题,即使在搜索了各种已解决的堆栈溢出问题后也是如此。

    public ActionResult UploadFtpFile(HttpPostedFileBase promoImgFile)
    {
        bool isSavedSuccessfully = true;
        string fileName = null;
        string completeDPath = "xxxxx";
        string username = "xxxx";
        string password = "xxxxx";
        FtpWebRequest ftpClient;
        FtpWebRequest ftpRequest;
        List<string> directories = new List<string>();

        try
        {
            foreach (string fileItemName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileItemName];
                fileName = file.FileName;
                if (file != null && file.ContentLength > 0)
                {
                    //Create FtpWebRequest object
                    ftpClient = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + completeDPath + "/" + fileName));
                    //Set request method
                    ftpClient.Method = WebRequestMethods.Ftp.UploadFile;

                    //Set Credentials
                    ftpClient.Credentials = new NetworkCredential(username, password);

                    //Opens a file to read
                    Stream objFile = file.InputStream;

                    //By default KeepAlive is true, where the control connection is not closed after a command is executed.
                    ftpClient.KeepAlive = true;

                    //Set the data transfer type
                    ftpClient.UseBinary = true;

                    //Set content length
                    ftpClient.ContentLength = file.ContentLength;

                    //Get Stream of the file
                    Stream objStream = ftpClient.GetRequestStream();

                    byte[] buffer = new byte[objFile.Length];
                    //Write file content
                    objStream.Write(buffer, 0, buffer.Length);

                    objStream.Close();
                    objFile.Close();

                }
            }
        }
        catch(Exception ex)
        {
            throw ex;
            isSavedSuccessfully = false;
        }

        //return Json(new { promoImgFile = directories });
        return RedirectToAction("Index", "Home", new { promoImgFile = directories });
    }

2 个答案:

答案 0 :(得分:1)

这就是这条线:

   Stream objFile = file.InputStream;

多年的学习,我找到了一件有用的东西:

MemoryStream ms = new MemoryStream();
file.InputStream.CopyTo(ms);

然后解决MemoryStream问题。

答案 1 :(得分:1)

示例(用于文本)

文档:MSDN

您问题的重要部分:

StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

示例(对于图像)

byte [] imageData = File.ReadAllBytes(imageSource);
request.ContentLength = imageData.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(imageData, 0, imageData.Length);
requestStream.Close();