开发者

how to attach file upload control on a button?

i have a Button on a HTML page . i want to attach file upload control on this button.

开发者_如何转开发i mean when a user click this button , file upload control will diaplay, and selected file will be uploaded.

tell me how can i do that?


You want to use the FileUpload control. That link to MSDN also has code samples to show how to save the selected file to the server.


"Finally ASP.NET 2.0 has a FileUpLoad control, which allows developers to drop the control on a page and let it browse a file and upload it on the server. To create a control, simply drop the FileUpload control from Toolbox to a Web page. The following code adds the FileUpLoad control:"

<asp:FileUpLoad id="FileUpLoad1" runat="server" />

"To support file upload, we need to add a Button control:"

<asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px" />

"Now on this button click event handler, we need to call SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded."

protected void UploadBtn_Click(object sender, EventArgs e)
{
if (FileUpLoad1.HasFile)
{

FileUpLoad1.SaveAs(@"C:\temp\" + FileUpLoad1.FileName);
Label1.Text = "File Uploaded: " + FileUpLoad1.FileName ;
}
else
{
Label1.Text = "No File Uploaded.";
}
}

http://asp.net-tutorials.com/controls/file-upload-control/
http://www.wrox.com/WileyCDA/Section/id-292158.html
http://www.c-sharpcorner.com/uploadfile/mahesh/fileupload10092005172118pm/fileupload.aspx


In order to upload files you must use

 <input type="file" />

Which has its own built-in button. The UI for this widget is difficult to control and varies widely between browsers. Implementing an attractive file uploading interface is one of those little joys all web developers experience sooner or later.

As usual, QuirksMode has a good writeup.


You need to use the <asp:FileUpload /> control for that. It renders an HTML control <input type="file" />

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜