I am creating webpart to upload file. I created asp button:

<asp:Button ID="txtUpload" runat="server" Text="Upload" OnClick="txtUpload_Click" />

Then on click event of button i write

FileUpload fileUpload = new FileUpload();

But i dont get any fileopen dialog. I also wish to add filter to only select images. How can we do it? After selecting i want to upload to sharepoint document list as attachment


I have found this example link but i want it without textbox and browse button. I just want to open file dialog on click of my button


try below code

<asp:FileUpload id="FileUploadControl" runat="server" /> <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />

to upload use below code in the event

string filename = Path.GetFileName(FileUploadControl.FileName);
        FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
        StatusLabel.Text = "Upload status: File uploaded!";
  • But this will display textbox and browse button – Happy May 7 '15 at 5:51
  • I don't think there is any way to open dailog for files as you wish to implement, you can try hiding the textbox and keep the button visible. – Akshay Randive May 7 '15 at 5:57
  • Can u tell how to hide textbox? – Happy May 7 '15 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.