locked
FileUpload Control Saving Files in Local Machine RRS feed

  • Question

  • User158811806 posted

    The code below always saves file in local machine, why?

    When it is run in local machine, it uploads to local machine. When I remote in to the server and run it locally on the server, it upload files on server's local drive.

    But, when I run it from my local server using this link "http://MyWebServer/MyWebSite/mainpage.aspx", it still uploads and saves files on my local machine.

    How do I get it to upload files on the server?

    protected void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
    
            if (FileUpload1.HasFile)
            {
                try
                {
                    sb.AppendFormat(" Uploading file: {0}", FileUpload1.FileName);
    
                    FileUpload1.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
    
                    sb.AppendFormat("<br/><br/> Uploaded: {0}", FileUpload1.PostedFile.FileName);
                }
                catch (Exception ex)
                {
                    sb.Append("<br/> Error <br/>");
                    sb.AppendFormat("Unable to upload file <br/> {0}", ex.Message);
                }
            }
            else
            {
                lblmessage.Text = sb.ToString();
            }
        }
    Friday, July 10, 2020 6:06 AM

Answers

  • User158811806 posted

    I've figured it out...

    I was using the localhost instead of using the actual server path

    Modified opendialog("http://localhost:12345/MyModal.aspx");

    As
    opendialog("http://MyWebServer/MyWebApp/MyModal.aspx");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 10, 2020 6:52 AM

All replies

  • User-821857111 posted

    Server.MapPath resolves a file path on the machine on which the code is executing. The URL that you use to reach the application is irrelevant.

    Friday, July 10, 2020 6:33 AM
  • User753101303 posted

    Hi,

    More likely a misunderstanding about what happens.. A web server can't save on a remote browser side drive without any user action. Could it be an older file from an earlier attempt? Use another file name or delete earlier attempts before trying again.

    Is suspect the catch block is triggered but as showing sb is in the else part, nothing  is shown and seeing and old file give the impression it was saved.

    Not directly related but showing exception messages to uses is likely not that a good idea. The usual approach is to show a generic message and log exception details at a place where those in charge of keeping the site up and running can easily monitor, diagnose and possibly fix that...

    Friday, July 10, 2020 6:48 AM
  • User158811806 posted

    I've figured it out...

    I was using the localhost instead of using the actual server path

    Modified opendialog("http://localhost:12345/MyModal.aspx");

    As
    opendialog("http://MyWebServer/MyWebApp/MyModal.aspx");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 10, 2020 6:52 AM
  • User753101303 posted

    This is triggered from a page on the same server? If yes you could write your app so that it works regardless of where it is installed without any code change (even for example if your app is installed a root web site ie site.com/myapp/MyModal.aspx

    Friday, July 10, 2020 6:58 AM