探索與發現

          研究java技術

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 109 評論 :: 0 Trackbacks
          ASP.NET Ajax中的UpdatePanel和asp.net中FileUpload控件是不兼容的,但是采用下面的方法,可以在ASP.NET+Ajax下實現文件上傳
          <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
               <Triggers>
               <asp:PostBackTrigger ControlID="btnUpload" />
               </Triggers>
          <ContentTemplate>
          <asp:FileUpload ID="FileUpload1" runat="server" Width="400px" />
          <asp:Button ID="btnUpload" runat="server" Text="上傳" OnClick="btnUpload_Click" />
          </ContentTemplate>
          </asp:UpdatePanel>
          把提交上傳文件的按鈕放在<Triggers>標簽內,而不放在<ContentTemplate>中,特別注意把UpdateMode設為Conditional。然后在btnUpload_Click方法中執行保存文件和寫入數據庫的操作。當然這樣做,上傳文件時就失去了異步交互的效果,但是,在執行其它操作的時候,都還是有Ajax特性的。另外,如果您的這個上傳文件的頁面使用了一個母版頁,并且在母版頁中有一個UpdatePanel,在母版頁中對子頁的控件加上<Triggers>是不行的,還好,UpdatePanel是可以嵌套的。




          方案一、如果將scriptmanager的enablepartialrending設為false則可以正確上傳,這個方法最簡單,但是會有缺陷,就是在同一個頁面上的多個UpdatePanel不可以獨自刷新了。另外,當你的UpdatePanel中存在Validator的話,會造成整個頁面postback,這個問題似乎是Altas的一個bug.

          方案二、摘自http://www.netfocus.cn/article950.html

          1:主頁面中使用UpdatePanel,然后UpdatePanel里面放置的不是FileUpload控件,而是一個Iframe
          2:這個iframe在鏈接一個新的頁面,那個頁面里面有FileUpload控件。
          3:上傳完畢后,告訴主頁面上傳得結果

          先看一個直接使用FileUpload的例子:這個例子里面,服務端是無法找到上傳文件的。

                      <atlas:UpdatePanel ID="up1" Mode="Conditional" runat="server">
                          
          <ContentTemplate>
                              
          <asp:FileUpload ID="FileUpload1" runat="server" />
                              
          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                           
          < SPAN>ContentTemplate>
                      
          < SPAN>atlas:UpdatePanel>


          看看,我們該如何實現
          1:新建主頁面Default.aspx
                在適當的位置,放置一個上傳附件的UpdatePanel區域
               

                      <atlas:UpdatePanel ID="up_attachment" Mode="Conditional" runat="server">
                          
          <ContentTemplate>
                             
          <iframe id="file" name="file" src="attachment.aspx">< SPAN>iframe>
                          
          < SPAN>ContentTemplate>
                      
          < SPAN>atlas:UpdatePanel>

          2:新建上傳文件的頁面attachment.aspx,然后放上FileUpload控件

          <div>
              
          <asp:FileUpload ID="FileUpload1" runat="server" />
                    
          <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />
              
          < SPAN>div>

          3:在attachment.aspx里面,上傳文件之后調用主頁面的js,報告上傳情況。這是函數原型:

              <script>
                window.top.callBack(fileName);
              
          < SPAN>script>

          4:Default.aspx主頁面里面增加這個函數,處理返回值
            

              <script>
               function callBack(fileName)
            
          {
                  document.getElementById(
          'Attach1').innerHTML=fileName;
               }

              
          < SPAN>script>
          posted on 2009-05-13 21:31 蜘蛛 閱讀(3630) 評論(2)  編輯  收藏 所屬分類: SharePoint

          評論

          # re: ASP.NET Ajax Uploadfile控件使用 2009-05-13 21:35 蜘蛛
          自定義用戶控件文件 PicUpload.ascx

          1<%@ control language="C#" autoeventwireup="true" inherits="Admin_PicUpload, App_Web_mboefw14" %>
          2 <asp:Image ID="EP_Image" runat="server" Height="160px" ImageUrl='<%# Bind("EP_Pic") %>'
          3 Width="314px" />
          4<br />
          5<asp:FileUpload ID="FileUpload1" runat="server" />
          6<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上傳" />
          自定義用戶控件文件 PicUpload.ascx.cs

          1using System;
          2using System.Data;
          3using System.Configuration;
          4using System.Collections;
          5using System.Web;
          6using System.Web.Security;
          7using System.Web.UI;
          8using System.Web.UI.WebControls;
          9using System.Web.UI.WebControls.WebParts;
          10using System.Web.UI.HTMLControls;
          11
          12public partial class Admin_PicUpload : System.Web.UI.UserControl
          13{
          14 protected void Page_Load(object sender, EventArgs e)
          15 {
          16
          17 }
          18 protected void Button1_Click(object sender, EventArgs e)
          19 {
          20 bool fileOK = false;
          21 //獲取根文件絕對路徑
          22 string path = Server.MapPath("~/UpLoad/");
          23 //如上傳了文件,就判斷文件格式
          24 FileUpload FU = FileUpload1;
          25 if (FU.HasFile)
          26 {
          27 string fileExtension = System.IO.Path.GetExtension(FU.FileName).ToLower();
          28 string[] allowedExtensions ={ ".gif", ".jpg", ".png", ".bmp", };
          29 for (int i = 0; i < allowedExtensions.Length; i++)
          30 {
          31 if (fileExtension == allowedExtensions[i])
          32 {
          33 fileOK = true;
          34 }
          35 }
          36 }
          37 //調用saveas方法,實現上傳文件
          38 if (fileOK)
          39 {
          40 try
          41 {
          42 FileUpload1.SaveAs(path + System.DateTime.Now.ToString("yyyyMMddhhmmss")+FU.FileName);
          43 EP_Image.ImageUrl = "../Upload/" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + FU.FileName;
          44 Button1.Text = "上傳成功";
          45 }
          46 finally
          47 {
          48 }
          49 }
          50 else
          51 {
          52 Button1.Text = "上傳失敗,格式不允許";
          53 }
          54 }
          55}
          56
            回復  更多評論
            

          # re: ASP.NET Ajax Uploadfile控件使用 2009-05-29 10:31 iven
          太感謝了  回復  更多評論
            

          主站蜘蛛池模板: 乌海市| 托里县| 上思县| 博客| 刚察县| 璧山县| 巴楚县| 偃师市| 兴文县| 梁河县| 潞城市| 卢氏县| 宕昌县| 伊金霍洛旗| 阿拉善盟| 昌都县| 航空| 南京市| 鸡泽县| 毕节市| 海南省| 万盛区| 淳安县| 涿鹿县| 铜川市| 南召县| 获嘉县| 白沙| 岳池县| 新巴尔虎左旗| 台中市| 衡南县| 同德县| 朝阳县| 永胜县| 萨嘎县| 吴忠市| 兴海县| 买车| 襄垣县| 中阳县|