c# - 为什么我不能在ftp服务器上上传?(仅在 IE 中,此代码适用于其他浏览器)
问题描述
具体来说,我提到的与我正在使用的代码一起使用的浏览器是:Opera、Mozilla 和 Google Chrome。但是在 IE 和 ME 上,我得到了这个错误。
550 No Such File or Directory
虽然在我提到的 3 个浏览器上此代码运行良好,但当我使用输入类型 =“文件”时,当我尝试在不使用 Web Exception 之前捕获错误时,我在第 37 行收到错误代码
Stream requestStream = request.GetRequestStream();
HTML
<form id="form1" runat="server">
<div>
<input type="file" id="fuAttachment" runat="server"/>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
代码背后
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://url/" + fuAttachment.PostedFile.FileName + "");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("username", "password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fuAttachment.PostedFile.InputStream);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Response.Write("File Successfully Uploaded");
response.Close();
}
catch (WebException a)
{
String status = ((FtpWebResponse)a.Response).StatusDescription;
Response.Write(status);
}
我该怎么做才能解决这个问题?我认为我不应该要求管理员更改 ftp 设置,因为此应用程序可以在其他浏览器上运行。
解决方案
检查事项:
IE 发送客户端的完整路径和文件名,而其他浏览器发送类似“C:\fakepath\filename”的内容。您不应依赖PostedFile.FileName
有效的文件名。
<input type="file">
需要(以前需要?)enctype='multipart/form-data'
作为包含的属性<form>
。
看到这个答案
推荐阅读
- asp.net - 无法将“System.Data.DataRowView”类型的对象转换为“QuestionBuilder”类型
- java - 两个凸包之间的最小距离最大化
- python - 将日期字段转换为一年中的某一天,然后将其添加到空字段中 arcpy python arcgis
- android - 统一项目作为库和 android 项目后,清单中出现错误
- cloudamqp - cloudampq 客户端未连接到 rabbitmq 服务器
- android - 将调用活动传递给寻呼机适配器
- c++ - 控制 std::map 顺序的最低要求
- c# - 如何获取列的值并将其放入标签 C# & SQL Server
- react-native - 反应原生 FlatList scrollToItem 错误
- julia - 将`limits`作为函数参数传递