繁体   English   中英

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

[英]File uploaded to ftp server is corrupted 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 });
    }

这条线就在这里:

   Stream objFile = file.InputStream;

多年的学习,我发现了一件有效的事情:

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

然后关闭MemoryStream。

示例(针对文字)

文件: 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM