C# webclient.UploadData“;基础连接已关闭:在接收器上发生意外错误;
我试图上传图像和视频使用下面的代码,没有问题的图像上传使用ftp,但当我上传视频我得到以下错误 错误C# webclient.UploadData“;基础连接已关闭:在接收器上发生意外错误;,c#,asp.net,C#,Asp.net,我试图上传图像和视频使用下面的代码,没有问题的图像上传使用ftp,但当我上传视频我得到以下错误 错误 The underlying connection was closed: An unexpected error occurred on a receive. 下面是我用来上传的代码 代码 try { string uploadFileName = Path.GetFileName(FU_Video.FileName);
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
这是堆栈跟踪
堆栈跟踪
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
搜索后,我读取使连接处于活动状态的值为false,但我在第行client.UploadData(uri,字节[])上得到错误代码>
请让我知道我的代码有什么问题?由于视频是通过ftp上传的,但我发现了错误。我记得有类似的问题,但不记得是什么让它工作的。以下是对我来说很有效的代码:
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);
}
}