Upload file code using FileUpload on UpdatePanel
@page language= "C #" masterpagefile= "~/evamanager.master" autoeventwireup= "true" codefile= "hv_excel.asp tutorial X.cs" inherits= "Hv_excel" title= "Untitled page"%>
<asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >
<script>
function callback (filename)
{
document.getElementById (' Attach1 '). Innerhtml=filename;
}
</script>
<fieldset>
<legend> leader and linkage object upload </legend>
<iframe id= "File" name= "file" src= "attachment.aspx" ></iframe>
<asp:textbox id= "Attach1" runat= "Server" ></asp:textbox>
</fieldset>
</asp:content>
Attachment.aspx
<%@ page language= "C #" autoeventwireup= "true" codefile= "Attachment.aspx.cs" inherits= "attachment"%>
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<script>
Window.top.callback (filename);
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "button1" runat= "Server" text= "OK" onclick= "Button1_Click"/>
</div>
</form>
</body>
Attachment.aspx.cs
using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.web.ui.webcontrols.webparts;
Using System.Web.UI.HtmlControls;
public partial class Attachment:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
if (Fileupload1.hasfile = = False)
{
Scriptmanager.registerstartups Tutorial Cript (This.page, This.gettype (), "Alert", "alert (' Unspecified upload file ');", true);
Return
}
if (fileupload1.filename.substring fileupload1.filename.lastindexof ('. ') + 1, 3). ToString (). ToLower ()!= "XLS")
{
Scriptmanager.registerstartupscript (This.button1, This.gettype (), "Alert", "alert (' File format Error!") Please make sure to download excel! for this page '); ", true);
Return
}
String Savepath = Request.physicalapplicationpath + "\upfile\" + fileupload1.filename;
Fileupload1.saveas (Savepath);
}
}
Method Two
<asp:updatepanel id= "UpdatePanel1" runat= "Server" >
<contenttemplate>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "button1" runat= "server" text= "upload" onclick= "Button1_Click"/>
</contenttemplate>
<triggers>
<asp:postbacktrigger controlid= "Button1"/>
</triggers>
</asp:updatepanel>
And if you want to do some tricks on this updatepanel, such as adding a asp:panel, can be triggered by button events hidden or displayed, you will find FileUpload1 can not find the file ...
In fact, the truth is very simple, updatepanel content is filled by XMLHTTP real-time, before you let him show, see the page source code inside is empty. A dynamic control update normal data no problem, but upload file is not, my solution is to use the ordinary div instead of Asp:panel, and wrote 2 functions to dynamically Send control script, button events in the call only the function can:
<div id= "Panel1" ></div>
private void Showpanel ()
{
String script = "document.getElementById (' Panel1 '). style.display= ';";
Scriptmanager.registerstartupscript (This.page, This.gettype (), "Showpanel", script, True);
}
private void Closepanel ()
{
String script = "document.getElementById (' Panel1 '). style.display= ' none ';";
Scriptmanager.registerstartupscript (This.page, This.gettype (), "Closepanel", script, True);
}