webclient.UploadData“基础连接已关闭:接收时发生意外错误”

时间:2013-05-03 09:34:55

标签: c# asp.net

我正在尝试使用下面的代码上传图片和视频,使用ftp上传图片没有问题,但是当我上传视频时,我收到以下错误

错误

The underlying connection was closed: An unexpected error occurred on a receive.

以下是我用来上传的代码

代码

 try
            {
                string uploadFileName = Path.GetFileName(FU_Video.FileName);
                uploadFileName = "video." + uploadFileName.Split('.')[1];
                using (WebClient client = new WebClient())
                {
                    string ftpAddres = "ftp://username:pasword@url-path" + fullname;

                    if (!GetAllFilesList(ftpAddres, "username", "password"))
                    {
                        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpAddres);
                        ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
                        ftp.KeepAlive = false;

                        FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
                    }
                    client.UploadData(new Uri(ftpAddres + "/" + uploadFileName), FU_Video.FileBytes);
                }

这是堆栈跟踪。

堆栈跟踪

   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
   at System.Net.WebClient.UploadData(Uri address, Byte[] data)
   at MentorMentee.SignUp.signup.btn_VidPreview_Click(Object sender, EventArgs e) in I:\VS Projects\code\MentorMentee1\MentorMentee\SignUp\signup.aspx.cs:line 469

搜索后我读取使连接生效为假,但我在第client.UploadData(uri,byte[]);

上收到错误

请让我知道我的代码有什么问题?视频在ftp上传,但我收到错误。

1 个答案:

答案 0 :(得分:1)

我记得有类似的问题,但不记得是什么让它起作用。以下是适用于我的代码:

public void Upload(Stream stream, string fileName)
{
    if (stream == null)
    {
        throw new ArgumentNullException("stream");
    }

    try
    {
        FtpWebRequest ftpRequest = CreateFtpRequest(fileName);
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

        using (Stream requestSream = ftpRequest.GetRequestStream())
        {
            Pump(stream, requestSream);
        }

        var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        ftpResponse.Close();
    }
    catch (Exception e)
    {
        throw new FtpException(
            string.Format("Failed to upload object. fileName: {0}, stream: {1}", fileName, stream), e);
    }
}

private FtpWebRequest CreateFtpRequest(string fileName)
{
    if (fileName == null)
    {
        throw new ArgumentNullException("fileName");
    }

    string serverUri = string.Format("{0}{1}", ftpRoot, fileName);
    var ftpRequest = (FtpWebRequest)WebRequest.Create(serverUri);
    ftpRequest.Credentials = new NetworkCredential(configuration.UserName, configuration.Password);
    ftpRequest.UsePassive = true;
    ftpRequest.UseBinary = true;
    ftpRequest.KeepAlive = false;

    return ftpRequest;
}

private static void Pump(Stream input, Stream output)
{
    var buffer = new byte[2048];
    while (true)
    {
        int bytesRead = input.Read(buffer, 0, buffer.Length);
        if (bytesRead == 0)
        {
            break;
        }
        output.Write(buffer, 0, bytesRead);
    }
}