无法打开文件进行上传; CreateFile()失败,错误123 / 0x7b

时间:2018-12-23 18:01:18

标签: c# windows ftp

我正在服务器上使用titan FTP将文件从本地计算机传输到服务器, 我收到此错误:

  

无法将数据写入传输连接:正在退出   连接被远程主机强行关闭。

我做错了什么?

private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
    try
    {
        string filename = ((FTPSetting)e.Argument).FileName;
        string fullname = ((FTPSetting)e.Argument).FullName;
        string username = ((FTPSetting)e.Argument).UserName;
        string password = ((FTPSetting)e.Argument).Password;
        string server = ((FTPSetting)e.Argument).Server + "/";

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", server, filename)));
        request.UsePassive = false;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        Stream ftpStreamRequest = request.GetRequestStream();
        FileStream fs = File.OpenRead(filename);
        byte[] buffer = new byte[1024];
        double total = (double)fs.Length;
        int byteRead = 0;
        double read = 0;
        do
        {
            if (!backgroundWorker1.CancellationPending)
            {
                byteRead = fs.Read(buffer, 0, 1024);
                ftpStreamRequest.Write(buffer, 0, byteRead);
                read += (double)byteRead;
                double percent = read / total * 100;
                backgroundWorker1.ReportProgress((int)percent);
            }
        } while (byteRead != 0);

        fs.Close();
        ftpStreamRequest.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

0 个答案:

没有答案