locked
Code for FileUpload control in vb.net RRS feed

  • Question

  • User1017948851 posted

    HI All, In my application Im using a textbox and button instead of fileupload control. I would like to know the code for the button so that onclick of button it opens a dialog box and takes the path that has been browsed. Kindly suggest

    Tuesday, April 17, 2012 9:25 AM

Answers

  • User-345793530 posted

    In my application, when user selects the file path using file upload control and clicks on some button, after the page loads I want to retain and display the file path in the fileupload control text box which user has selected which Im not able to get that. So I was checking the other way. If you could suggest me how to retain value into the textbox after page load then that satisfies my requirement. 

    Thanks

    Actually, you can not get the full path of the uploaded file through FileUpload.PostedFile.FileName from server.  It just a file name. this feature has been disabled due to security reasons on most browsers. Any website should not need path of a file stored in client's systems because that gives the directory structure and may expose other important things to website owner.

    If you want to upload files without using the FileUpload server control, you can use input tag instead of FileUpload control.Please look at the following simple example:

     <input type="file" id="myFile" name="myFile" />
     <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
            protected void btnUploadClick(object sender, EventArgs e)
            {
                HttpPostedFile file = Request.Files["myFile"];
                if (file != null && file.ContentLength>0)
                {
                    string fname = Path.GetFileName(file.FileName);
                    file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
                }
            } 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 28, 2012 3:00 AM

All replies

  • User465171450 posted

    You can't do this. You have to use the FileUpload control. That's what it is there for as it emits the proper html field for uploading. There is no code that gets this behavior to happen, it's part of the HTML standard and requires the proper elements in order to work so you can't do this with a textbox and button.

    Tuesday, April 17, 2012 9:44 AM
  • User1958125286 posted

    This should help you, 

    http://www.4guysfromrolla.com/webtech/091201-1.shtml

    Tuesday, April 17, 2012 9:49 AM
  • User-691245060 posted

    Probably using some CSS and javascript tricks you can achieve this...again I am not sure....but why would you want to do this??? When you have asp.net fileuplod control and infact html file type control, why you want to reinvent the wheel??

    but still you want to give a try....put up a html file type control and make it hidden, then on button client click stimulate hidden controls click event using javascript...having said that I dont know whether you can post the file to server or not,...give it a try and let me know...

    Thanks,

    Tuesday, April 17, 2012 10:24 AM
  • User-424641356 posted

    While you have FileUpload Tool available and you can use that. Why do you want to waste your time?

    Tuesday, April 17, 2012 10:31 AM
  • User1085077483 posted

    see the link below

    http://www.c-sharpcorner.com/UploadFile/mahesh/FileUpload10092005172118PM/FileUpload.aspx

    Tuesday, April 17, 2012 10:32 AM
  • User1017948851 posted

    In my application, when user selects the file path using file upload control and clicks on some button, after the page loads I want to retain and display the file path in the fileupload control text box which user has selected which Im not able to get that. So I was checking the other way. If you could suggest me how to retain value into the textbox after page load then that satisfies my requirement.

     

    Thanks

    Thursday, April 19, 2012 6:29 AM
  • User1958125286 posted

    You can actually take a label or textbox whatever you want and just populate the value of the FileUpload. 

    You can do this when the FileUpload Control is posting the data to the server. 

    use..

     System.IO.Path.GetDirectoryName(Server.MapPath(FileUpload1.PostedFile.FileName));


    Thursday, April 19, 2012 9:05 AM
  • User-345793530 posted

    In my application, when user selects the file path using file upload control and clicks on some button, after the page loads I want to retain and display the file path in the fileupload control text box which user has selected which Im not able to get that. So I was checking the other way. If you could suggest me how to retain value into the textbox after page load then that satisfies my requirement. 

    Thanks

    Actually, you can not get the full path of the uploaded file through FileUpload.PostedFile.FileName from server.  It just a file name. this feature has been disabled due to security reasons on most browsers. Any website should not need path of a file stored in client's systems because that gives the directory structure and may expose other important things to website owner.

    If you want to upload files without using the FileUpload server control, you can use input tag instead of FileUpload control.Please look at the following simple example:

     <input type="file" id="myFile" name="myFile" />
     <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
            protected void btnUploadClick(object sender, EventArgs e)
            {
                HttpPostedFile file = Request.Files["myFile"];
                if (file != null && file.ContentLength>0)
                {
                    string fname = Path.GetFileName(file.FileName);
                    file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
                }
            } 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 28, 2012 3:00 AM