文件上传的文件名一个UpdatePanel的局部回传后是空的? [英] FileName of fileupload is empty after a partial postback of an updatepanel?

查看:195
本文介绍了文件上传的文件名一个UpdatePanel的局部回传后是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UpdatePanel的一个FileUpload。

 < ASP:的UpdatePanel ID =UpdatePanel1=服务器的UpdateMode =始终>
<&触发器GT;
    < ASP:PostBackTrigger控件ID =SaveButton/>
< /触发器>
<&的ContentTemplate GT;
< CC1:TabContainer的的CssClass =visoft__tab_xpie7=服务器ID =标签ActiveTabIndex =0WIDTH =100%FONT-SIZE =11像素>
 < CC1:的TabPanel =服务器的HeaderText =اطلاعاتپایهID =TabPanel1>
     <&的ContentTemplate GT;
      < D​​IV CLASS =行>
       <跨度风格=WIDTH:100px的阶级=rowtitle>تصویر< / SPAN>
        < ASP:文件上传ID =ImageFileUpload=服务器/>
        < ASP:ImageButton的ID =RemoveImageButton=服务器的ImageUrl =〜/图片/ delete.png/>
      < / DIV>
      < D​​IV CLASS =行>
       <跨度风格=WIDTH:100px的阶级=rowtitle>分类和LT; / SPAN>
       < ASP:DropDownList的ID =CategoryDropDownList=服务器WIDTH =200像素的AutoPostBack =真DataTextField =名称DataValueField =IDOnSelectedIndexChanged =CategoryDropDownList_SelectedIndexChanged/>
       < ASP:的RequiredFieldValidator ID =CategoryRFV=服务器的ControlToValidate =CategoryDropDownList前景色=红的ValidationGroup =保存的ErrorMessage =مجموعهراانتخابکنید。文本=*/>
      < / DIV>
      < D​​IV CLASS =行的风格=高度:300像素>
       <跨度风格=WIDTH:100px的阶级=rowtitle>توضیحاتکامل< / SPAN>
       <主编:HTML编辑风格=浮动:左=服务器WIDTH =600px的ID =DescriptionHtmlEditor/>
       < ASP:文本的RequiredFieldValidator =*ID =DescriptionRFV=服务器的ControlToValidate =DescriptionHtmlEditor前景色=红的ValidationGroup =保存的ErrorMessage =توضیحاتراواردکنید。 />
       < / DIV>
     < /&的ContentTemplate GT;
    < / CC1:&的TabPanel GT;
   < / CC1:TabContainer的>
   < D​​IV CLASS =行的风格=文本对齐:中心>
    < ASP:按钮的ID =SaveButton级=按钮WIDTH =100像素的OnClick =SaveButton_Click=服务器的ValidationGroup =保存文本=ذخیره/>
   < / DIV>
  < /&的ContentTemplate GT;
  < / ASP:的UpdatePanel>

当我试图一个UpdatePanel的局部回传后访问的FileUpload FileName属性,它是空的。

我要得到uploadpanel的Filenme在oncange事件witj的javascrip,但我没有得到文件的完整路径。

 函数FileChange()
   {
    变种文件名=的document.getElementById('<%= FileNameUpload.C​​lientID%GT;');
    var文件=的document.getElementById('文件上传');
    filename.value = file.value;
   }

如何局部回传后得到文件上传的文件名?


解决方案

  

当我试图后访问的FileUpload FileName属性
  一个UpdatePanel的局部回传,它是空的。


更新面板简单地拦截普通表单提交和执行完全回发使用XMLHTTP请求。正因为如此,选择最初提出的输入:文件元素在初始回传已经提交,您无法读取它了之后。为了证明什么,我说,尝试和初始回发读取的文件名,你会看到,你就可以。

看你的code,它也好像你正试图设置文件上传元素的值编程。这是不可能的或者出于安全原因 - 你不能使用JavaScript访问用户的硬盘驱动器,并决定哪些文件的用户应上传到你的网站。一旦用户选择的文件,并提交表单,选择被消失了;用户将不得不重新选择自己的文件。

您可以尝试很多黑客做与UpdatePanel的这个工作,但是从长远来看,最干净的,是落实这个使用jQuery的Ajax,因为你有什么被提交完全控制,什么不和的时候。

我希望这有助于澄清为什么你的做法是行不通的。

I have a FileUpload in the Updatepanel.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
    <asp:PostBackTrigger ControlID="SaveButton" />
</Triggers>
<ContentTemplate>
<cc1:TabContainer CssClass="visoft__tab_xpie7" runat="server" ID="tab" ActiveTabIndex="0" Width="100%" Font-Size="11px">
 <cc1:TabPanel runat="server" HeaderText="اطلاعات پایه" ID="TabPanel1">
     <ContentTemplate>
      <div class="row">
       <span style="width: 100px" class="rowtitle">تصویر </span>
        <asp:FileUpload ID="ImageFileUpload" runat="server"/>
        <asp:ImageButton ID="RemoveImageButton" runat="server" ImageUrl="~/images/delete.png" />
      </div>
      <div class="row">
       <span style="width: 100px" class="rowtitle">Category</span>
       <asp:DropDownList ID="CategoryDropDownList" runat="server" Width="200px" AutoPostBack="True" DataTextField="Name" DataValueField="ID" OnSelectedIndexChanged="CategoryDropDownList_SelectedIndexChanged" />
       <asp:RequiredFieldValidator ID="CategoryRFV" runat="server" ControlToValidate="CategoryDropDownList" ForeColor="Red" ValidationGroup="Save" ErrorMessage="مجموعه را انتخاب کنید." Text="*" />
      </div>
      <div class="row" style="height: 300px">
       <span style="width: 100px" class="rowtitle">توضیحات کامل</span>
       <editor:HtmlEditor Style="float: left" runat="server" Width="600px" ID="DescriptionHtmlEditor" />
       <asp:RequiredFieldValidator Text="*" ID="DescriptionRFV" runat="server" ControlToValidate="DescriptionHtmlEditor" ForeColor="Red" ValidationGroup="Save" ErrorMessage="توضیحات را وارد کنید ." />
       </div>
     </ContentTemplate>
    </cc1:TabPanel>
   </cc1:TabContainer> 
   <div class="row" style="text-align: center">
    <asp:Button ID="SaveButton" class="button" Width="100px" OnClick="SaveButton_Click" runat="server" ValidationGroup="Save" Text="ذخیره" />
   </div>
  </ContentTemplate>
  </asp:UpdatePanel>

when i was trying to access the FileUpload FileName property after a partial postback of an updatepanel, it was empty.

I want get Filenme of uploadpanel in oncange event witj javascrip, but i don't get Fullpath of file.

 function FileChange()
   {
    var filename = document.getElementById('<%= FileNameUpload.ClientID  %>');
    var file = document.getElementById('fileupload');
    filename.value = file.value;
   }

How to get filename of fileupload after partial postback?

解决方案

When i was trying to access the FileUpload FileName property after a partial postback of an updatepanel, it was empty.

Update Panels simply intercept the regular form submit and perform a FULL postback using an xmlhttp request. Because of this, the selection initially made on the input:file element was already submitted in the initial postback and you can't read it anymore afterwards. To prove what I am saying, try and read the file name on the initial postback and you'll see that you'll be able to.

Looking at your code, it also seems that you are trying to set the value of fileupload element programmatically. This is not possible either for security reasons - you can't access a user's hard drive from Javascript and decide which file the user should upload to your website. Once the user selected the file and the form was submitted, the selection is gone; the user would have to re-select the file himself.

You can try many hacks to make this work with UpdatePanels, but the cleanest in the long run is to implement this using jQuery Ajax because you have complete control of what gets submitted and what doesn't and when.

I hope this helps clarifying why your approach won't work.

这篇关于文件上传的文件名一个UpdatePanel的局部回传后是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆