Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dmitrij Jazel 86 posts 179 karma points
    Mar 16, 2013 @ 15:38
    Dmitrij Jazel
    0

    File upload through custom user control

    Hej Guys,

    Have a really basic task here...

    I have a blank new UserControl - where I have simple asp:FileUpload control...

     

    What I want is - upload actual file to the server. And save it there.

    Then if I need it, it should have a url - that links to the document.

     

    My problem is. when I am trying to use this

    string fileName = fileUpload.FileName;

    As a result - I end up having string value, that links to no file, because file was not actually uploaded.

     

    My ultimate goal is to make a control that can do the same thing as "upload" Document type property 

    I want to make 1 like this myself programatically.

    Any suggestions how to do something like this?

     

  • Wouter Dewispelaere 5 posts 47 karma points
    Mar 21, 2013 @ 23:16
    Wouter Dewispelaere
    0

    Hey Dmitrij,

    looks like you are looking for regular .NET functionality.

    You would need a button to trigger the actual upload of the file.

    So your ascx could look like this:

    <asp:FileUpload ID="OurUmbr_FileUpload" runat="server" />
    <asp:Button ID="OurUmbr_UploadButton" runat="server" Text="Upload file" OnClick="OurUmbr_UploadButton_Click" />
    <asp:Label ID="OurUmbr_UploadStatusLabel" runat="server" />

    And codebehind could look like:

       protected void OurUmbr_UploadButton_Click(object sender, EventArgs e)
        {
          if (!OurUmbr_FileUpload.HasFile) return;
          try
          {
            var filename = Path.GetFileName(OurUmbr_FileUpload.FileName);
            OurUmbr_FileUpload.SaveAs(Server.MapPath("~/") + filename);
            OurUmbr_UploadStatusLabel.Text = "Upload status: File uploaded!";
          }
          catch (Exception ex)
          {
            OurUmbr_UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
          }
        }
    

     

    This should do the trick.

    Take care,

    WD

Please Sign in or register to post replies

Write your reply to:

Draft