none
The SaveAs method is configured to require a rooted path, and the path is not rooted

    Question

  • hi guys,

    i want to upload a file to my ftp server.i use code that it is below :

    if (FileUpload1.HasFile)
                {
                    string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                    FileUpload1.SaveAs(request.MapPath + FileUpload1.FileName);

                    FileUpload1.SaveAs("ftp://myftp.com" + FileUpload1.FileName);

                }

    but i take some error

     

    "The SaveAs method is configured to require a rooted path, and the path 'ftp://myftp.com/Links.txt' is not rooted."

     

    when i want to upload it to c:\\ , it's work.

    what's problem?anybody help me?

     

     


    TuNCeRo
    • Edited by TuNCeRo Wednesday, July 21, 2010 12:08 PM false information
    Wednesday, July 21, 2010 11:28 AM

Answers

  • You cannot use the SaveAs method to write directly to an FTP server. Only local paths and UNC paths are supported.

     

    To save it to FTP you'll have to use the FtpWebRequest class. See:

    http://www.vcskicks.com/csharp_ftp_upload.php

    You can use FileUpload.PostedFile.InputStream to access the data without having to save it to disk first.

    Wednesday, July 21, 2010 12:36 PM
  • I think the problem comes from the name FileUpload control. This control isn't used to upload files from your webserver to another location, but it's used to upload a file *to* your webserver from the client.

    So as written in the previous post the FileUpload control implements this:

    (Webbrowser on client) ----> (webserver running your application)

    You can then use the uploaded file to either Save it to disk, or load it into memory.

    To save it to disk, you can use the SaveAs() method, to do anything else you'll have to load the file into memory and take it from there.

    (Webbrowser on client) ----> (webserver running your application) ----> (ftp server)

    to implement the second part you an use the following code:

    using System.Net;
    using System.IO;
    
        string ftpBaseAddress = @"ftp://ftp.yoursite.com/folder";
        string username = "yourusername";
        string password = "yourpassword";
    
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpBaseAddress + "/" + Path.GetFileName(FileUpload1.PostedFile.FileName));
    
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
    
        using (Stream destination = request.GetRequestStream())
        {
          FileUpload1.PostedFile.InputStream.CopyTo(destination);
          destination.Flush();
        }
    

    Thursday, July 22, 2010 8:52 AM
  • Hi TuNCeRo!

    I think you are having trouble in uploading file to ftp server.

    I will explain process of uploading file to ftp server.

    if you are uploading any file from anywhere to Ftp Server directly is not possible.

    The Process is ,you are uploading file it means that first it goes to your Server(i.e., you project is deployed and Running Machine) temporary Path.

    From temporary path only you can upload your file to FTP Server.

    After uploading you can delete the tempory Path file.

    RemoteMachine-------------uploading File----->ur Project(TemporaryPath)------>Ftp Server

    Hopes this information helps you. Kindly let me know if I'm Wrong

     

    Thanks in Advance

    K.Navaneethapperumal

     

     

    Thursday, July 22, 2010 6:58 AM

All replies

  • anybody home?? (:

    TuNCeRo
    Wednesday, July 21, 2010 12:22 PM
  • You cannot use the SaveAs method to write directly to an FTP server. Only local paths and UNC paths are supported.

     

    To save it to FTP you'll have to use the FtpWebRequest class. See:

    http://www.vcskicks.com/csharp_ftp_upload.php

    You can use FileUpload.PostedFile.InputStream to access the data without having to save it to disk first.

    Wednesday, July 21, 2010 12:36 PM
  • but i use fileupload tool from toolbox.how can i do?

    when i write c:\\test.txt it's work.but i use fileupload tool,,have some error :s


    TuNCeRo
    • Edited by TuNCeRo Wednesday, July 21, 2010 1:17 PM extra information
    Wednesday, July 21, 2010 1:00 PM
  • please,  help me..
    TuNCeRo
    Thursday, July 22, 2010 6:40 AM
  • Hi TuNCeRo!

    I think you are having trouble in uploading file to ftp server.

    I will explain process of uploading file to ftp server.

    if you are uploading any file from anywhere to Ftp Server directly is not possible.

    The Process is ,you are uploading file it means that first it goes to your Server(i.e., you project is deployed and Running Machine) temporary Path.

    From temporary path only you can upload your file to FTP Server.

    After uploading you can delete the tempory Path file.

    RemoteMachine-------------uploading File----->ur Project(TemporaryPath)------>Ftp Server

    Hopes this information helps you. Kindly let me know if I'm Wrong

     

    Thanks in Advance

    K.Navaneethapperumal

     

     

    Thursday, July 22, 2010 6:58 AM
  • I think the problem comes from the name FileUpload control. This control isn't used to upload files from your webserver to another location, but it's used to upload a file *to* your webserver from the client.

    So as written in the previous post the FileUpload control implements this:

    (Webbrowser on client) ----> (webserver running your application)

    You can then use the uploaded file to either Save it to disk, or load it into memory.

    To save it to disk, you can use the SaveAs() method, to do anything else you'll have to load the file into memory and take it from there.

    (Webbrowser on client) ----> (webserver running your application) ----> (ftp server)

    to implement the second part you an use the following code:

    using System.Net;
    using System.IO;
    
        string ftpBaseAddress = @"ftp://ftp.yoursite.com/folder";
        string username = "yourusername";
        string password = "yourpassword";
    
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpBaseAddress + "/" + Path.GetFileName(FileUpload1.PostedFile.FileName));
    
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
    
        using (Stream destination = request.GetRequestStream())
        {
          FileUpload1.PostedFile.InputStream.CopyTo(destination);
          destination.Flush();
        }
    

    Thursday, July 22, 2010 8:52 AM
  • Hi Jesse,

     

    Thanks for the code.

     

    But i am facing two problems here.

     

     FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpBaseAddress + "/" + Path.GetFileName(FileUpload1.PostedFile.FileName));

    the above line is throwing an exception

     

    "Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.FtpWebRequest'"

     

    And

     

     FileUpload1.PostedFile.InputStream.CopyTo(destination);

    And an error error in the above statement. Saying CopyTo is not a method.

     

    Please help me on this.

     

    Thanks

     

    Sreeni pavalla

    Tuesday, July 27, 2010 4:38 PM
  • Stream.CopyTo is new in Framework 4, either upgrade your project to Visual Studio 2010, or go back to the artice I posted, it explains how to copy the data from one stream to another.

    http://www.vcskicks.com/csharp_ftp_upload.php

    Are you sure you're passing int he correct address to ftpBaseAddress? It should look something like:

     

    ftp://myserver.com/path

    The FileWebRequest results from pssign in a path like:

    c:\path

    or 

    file://myserver.com/path

    • Proposed as answer by Jesse Houwing Wednesday, July 28, 2010 6:27 AM
    Wednesday, July 28, 2010 6:26 AM
  • Very nice :)
    Wednesday, October 16, 2013 7:41 PM