首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webclient.UploadData“基础连接已关闭:在接收端发生意外错误”

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

Stack Overflow用户
提问于 2013-05-03 09:34:54
回答 1查看 1.5K关注 0票数 0

我试图使用下面的代码上传图像和视频,使用ftp上传图像没有问题,但是当我上传视频时,我会得到以下错误

误差

代码语言:javascript
复制
The underlying connection was closed: An unexpected error occurred on a receive.

下面是我用来上传的代码

代码语言:javascript
复制
 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);
                }

这是堆栈跟踪。

堆栈跟踪

代码语言:javascript
复制
   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上,但我得到了错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-06 04:30:38

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

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16355807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档