FtpWebRequest while writing to stream if network goes down then FTP Server gives access denied problem when tried to re upload

你。 提交于 2019-12-24 21:46:59

问题


I am using FtpWebRequest to upload a file. Facing a problem of which i am not getting solution.

While uploading a heavy file if network get disconnected then FTP server acquires lock on file being uploaded, now when user tries to re upload the same file then it get access denied error.

I have set TimeOut and ReadWriteTimeOut to 5 secs of FtpWebRequest on FTP Server it is 10 secs. Even if i try to upload same file after an Hour then also same problem exist.

 // Get the object used to communicate with the server.
  request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpInfo.FtpServer.Trim() + "/" + FtpInfo.FtpFolderPath.Trim() + "/" + FileName.Trim()));
   request.Method = WebRequestMethods.Ftp.UploadFile;
   request.Proxy = null;

   // FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential(FtpInfo.UserNameForFTP.Trim(), FtpInfo.PasswordForFTP.Trim());
    request.UsePassive = FtpInfo.UsePassive;
    request.KeepAlive = FtpInfo.KeepAlive;
    request.Timeout = FtpInfo.TimeOut; //Milliseconds
    request.UseBinary = true;
    request.ReadWriteTimeout = FtpInfo.TimeOut; //Milliseconds

                        FileInfo fi = new FileInfo(SourceLocation);
                        long length = fi.Length;
                        BytesUploaded = length;

                        long uploadSize = 0;
                        if (chunks == 0)
                        {
                            chunks = 1024;
                        }
                        else
                        {
                            buffLength = chunks;
                        }
                        byte[] buff = new byte[buffLength];
                        int contentLen;
                        using (FileStream fs = fi.OpenRead())
                        {
                            using (Stream strm = request.GetRequestStream())
                            {
                                contentLen = fs.Read(buff, 0, buffLength);
                                try
                                {
                                    while (contentLen != 0)
                                    {
                                        Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { lblProgress.Content = "Uploading '" + FileName + "'......" + "Bytes Uploaded (" + uploadSize.ToString() + "/" + length.ToString() + ")"; });
                                        strm.Write(buff, 0, contentLen);
                                        uploadSize += contentLen;
                                        contentLen = fs.Read(buff, 0, buffLength);
                                    }
                                    strm.Close();
                                }
                                catch (Exception ex)
                                {
                                    if (strm!=null)
                                    {
                                        try
                                        {
                                            strm.Close();
                                        }
                                        catch
                                        {
                                            throw ex;
                                        }
                                    }
                                    throw ex;
                                }
                            }
                            fs.Close();
                        }


                        try
                        {
                            //requestStream.Close(); -orignal
                            fi = null;
                            request=null;
                        }
                        catch { }

回答1:


I don't know enough about your app (is the FTP upload on a separate thread for example) but try this:

bool DoneOK = false;
FtpWebRequest request = null;
FtpWebResponse response = null;
try {
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + FtpInfo.FtpServer.Trim() + "/" + FtpInfo.FtpFolderPath.Trim() + "/" + FileName.Trim()));
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Proxy = null;

    // FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential(FtpInfo.UserNameForFTP.Trim(), FtpInfo.PasswordForFTP.Trim());
    request.UsePassive = FtpInfo.UsePassive;
    request.KeepAlive = FtpInfo.KeepAlive;
    request.Timeout = FtpInfo.TimeOut; //Milliseconds
    request.UseBinary = true;
    request.ReadWriteTimeout = FtpInfo.TimeOut; //Milliseconds

    long length = new FileInfo(SourceLocation).Length;
    long uploadSize = 0;
    if ( chunks < 1 )
         chunks = 1024;

    buffLength = chunks;
    byte[] buff = new byte[buffLength];
    int contentLen = 0;
    string MSG = "";

    using (FileStream fs = File.OpenRead (SourceLocation))
    using (Stream strm = request.GetRequestStream())
    {
        while ((contentLen = fs.Read(buff, 0, buffLength)) > 0 )
        {
        MSG = "Uploading '" + FileName + "'......" + "Bytes Uploaded (" + uploadSize.ToString() + "/" + length.ToString() + ")";
        string tmp_MSG = MSG;
        Dispatcher.Invoke(DispatcherPriority.Normal, () => { lblProgress.Content = tmpMSG; });
        strm.Write(buff, 0, contentLen);
        uploadSize += contentLen;
        };

        strm.Close();

        // necessary - the upload occurs really here !
        response = (FtpWebResponse) request.GetResponse();
        // check the response codes... for example FileActionOK...
        if ( response.StatusCode == System.Net.FtpStatusCode.FileActionOK )
             DoneOK = true;

        response.Close(); response = null;
        request = null;
    }
    }
catch
{
};

if ( request != null )    
     {
     if ( response != null )
          {
          try { response.Close(); } catch {};
          response = null;
          }

     if ( !DoneOK )
          {
          try { request.Abort (); } catch {};
          }

     request = null;
     }



回答2:


This was problem related to the FTP Server we were using, when we switched FTP server to IIS then everything worked smoothly. Thanks for all the help



来源:https://stackoverflow.com/questions/7144180/ftpwebrequest-while-writing-to-stream-if-network-goes-down-then-ftp-server-gives

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!