首页 > 解决方案 > 为什么我不能在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 设置,因为此应用程序可以在其他浏览器上运行。

标签: c#asp.net

解决方案


检查事项:

IE 发送客户端的完整路径和文件名,而其他浏览器发送类似“C:\fakepath\filename”的内容。您不应依赖PostedFile.FileName有效的文件名。

<input type="file">需要(以前需要?)enctype='multipart/form-data'作为包含的属性<form>

看到这个答案


推荐阅读