Upload large file to ftp C#

2020-04-18 06:43发布

I'm uploading large files to ftp site using this code.

Code

using (FileStream fs = new FileStream(FileLoc, FileMode.Open, FileAccess.Read))
            {
                string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
                FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
                requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                requestObj.Credentials = new NetworkCredential(Uid, Pass);
                using (Stream requestStream = requestObj.GetRequestStream())
                {

                    byte[] buffer = new byte[8092];
                    int read = 0;

                    while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        requestStream.Write(buffer, 0, read);
                    }

                    requestStream.Flush();
                    if (fs != null)
                    {
                        fs.Close();
                        fs.Dispose();
                    }
                    if (requestStream != null)
                    {
                        requestStream.Close();
                        requestStream.Dispose();
                    }
                }
            }

Some times this code up-lode files very fine but some time it up-lodes some part of file not complete file and doesn't give any error.

Can any one help me why some time it upload only some part of file not hole file.

标签: c# ftp
1条回答
我欲成王,谁敢阻挡
2楼-- · 2020-04-18 07:19

Here's the code we use for uploading to FTP. It looks very similar to your own. Nevertheleess, I post it for your reference as we haven't had any such reported failures

    private void UploadFile(string fileToUpload)
    {
        Output = new Uri(Path.Combine(Output.ToString(), Path.GetFileName(fileToUpload)));
        FtpWebRequest request = WebRequest.Create(Output) as FtpWebRequest;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        // in order to work with Microsoft Windows Server 2003 + IIS, we can't use passive mode.
        request.UsePassive = false;
        request.Credentials = new NetworkCredential(username, password);

        // Copy the contents of the file to the request stream.
        Stream dest = request.GetRequestStream();
        FileStream src = File.OpenRead(fileToUpload);

        int bufSize = (int)Math.Min(src.Length, 1024);
        byte[] buffer = new byte[bufSize];
        int bytesRead = 0;

        int numBuffersUploaded = 0;            

        do
        {
            bytesRead = src.Read(buffer, 0, bufSize);
            dest.Write(buffer, 0, bufSize);
            numBuffersUploaded++;
        }
        while (bytesRead != 0);

        dest.Close();
        src.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        if (response.StatusCode != FtpStatusCode.ClosingData)
        {
            Console.Error.WriteLine("Request {0}: Error uploading file to FTP server: {1} ({2})", Id, response.StatusDescription, response.StatusCode);
        }
        else
        {
            Console.Out.WriteLine("Request {0}: Successfully transferred file to {1}", Id, Output.ToString());
        }
    }
查看更多
登录 后发表回答