1

Specifically the browsers that I mentioned that works with the code I am using is this: Opera, Mozilla, & Google Chrome. But on IE, and ME I get this error.

550 No Such File or Directory

While on the 3 browsers that I had mentioned this code works well, when I use the input type = "file", when I tried catching the error before without using the Web Exception, I got the error on line 37 which has this code

  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>

Code Behind

  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);
        }

What do I have to do to fix this problem? I don't think that I should be asking for the administrator to change the ftp settings, because this application works on the other browsers.

Raniel Quirante
  • 431
  • 2
  • 12

2 Answers2

4

Things to check:

IE sends the client's full path and filename, whereas other browsers send something like "C:\fakepath\filename". You should not rely on PostedFile.FileName being a valid file name.

<input type="file"> requires (used to require?) enctype='multipart/form-data' as attribute of the containing <form>.

See this answer

devio
  • 36,858
  • 7
  • 80
  • 143
  • so instead of PostedFile.FileName what do you suggest that I should use? also, I did enctype=multipart/form-data in my form it still has the same error. I did try removing the fuattachment.postedfile.filename and changed it to a string "a" and it works. – Raniel Quirante May 04 '18 at 05:43
  • "sanitize user input": You cannot rely on the file name provided by the browser. use `Path.GetFilename()` to extract the pure filename, handle characters not supported by OS. – devio May 04 '18 at 06:21
  • Okay, I used substring to extract the file name from the fuattachment.posted.filename, thanks a lot for the advise, it really helped me. – Raniel Quirante May 04 '18 at 06:32
  • but I think I'll with Path.Getfilename since I got an error when I am using Google Chrome now – Raniel Quirante May 04 '18 at 06:34
0

Have you tried setting these options?

How to change the Internet Explorer FTP Client mode

  1. Start Internet Explorer.
  2. On the Tools menu, click Internet Options.
  3. Click the Advanced tab.
  4. Under Browsing, click to clear the Enable folder view for FTP sites check box.
  5. Click to select the Use Passive FTP (for firewall and DSL modem compatibility) check box.

It could be that the other browsers are set to use passive mode and IE isn't or vice versa

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • I tried the solution that you suggested, but I still got the same error. "Use Passive FTP" by default is checked, Also "Enable folder view" but I cleared it as it was suggested. – Raniel Quirante May 04 '18 at 02:40