如何在ASP.Net中更改Fileupload控件时获取Filename? [英] How to get Filename on change of Fileupload control in ASP.Net?

查看:215
本文介绍了如何在ASP.Net中更改Fileupload控件时获取Filename?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了文件上传控件。

现在,我想在用户选择文件后立即在文本框中显示Fileupload控件中选择的文件名。

需要快速回复。

解决方案

ok
文件上传控件有一个名为FileName的属性,你可以检索一个selceted文件的名称

然而,还有另一个名为postingFile的proerty,这将检索一个类型为httpPostFile对象的对象,然后它有一个迷宫的属性为



FileUpload obj; // aleady存在于网络表单中

//将您的页面发送回服务器

 如果(mypage.IspostedBack)
{
if (obj.hasFile)
{httppostedFile obj2 = obj.postedFile;
obj2.fileName
obj2.contentType
}
}

依此类推..

谢谢你





Ankur:您提供的代码需要回发。

[/ Edit]


你可以使用javascript通过上传控制来显示所选文件



 <   div     class   = 控制 >  
< asp:fileupload id = filelogo < span class =code-attribute> runat = server onchange = callme() xmlns:asp = #unknown >
< / asp:fileupload > < / div >



在此处添加文本框

 <   pre     lang   =  HTML >  <   div     class   = 控制 >  
< asp:FileUpload ID = filelogo < span class =code-attribute> runat = server onchange = callme(this) >
< / div > < / pre >
< asp:textbox id = txtUploadedFileName runat = server xmlns:asp = < span class =code-keyword>#unknown > < / asp:textbox >





现在在.aspx文件上写这个函数



 < < /跨度> <跨度class =code-leadattribute> script     type   =  text / javascript >  
function callme()
{
document .getElementById(' <%= txtUploadedFileName.ClientID%>')。value = document .getElementById(' <%= filelogo.ClientID%>')。value;

}

< / script > < / script >





一旦你选择了该文件的uploadcontrol名称中的任何文件将反映在文本框中。


 function callme(oFile){
document.getElementById( TextBox1)。 value = oFile。 value ;
}









 <   asp:FileUpload    < span class =code-attribute> ID   =  FileUpload1    runat   =  server      onchange   =  callme(this)   < span class =code-keyword> /  >  
< asp:TextBox ID = TextBox1 runat = server > < / asp:TextBox >







Ankur:但这会给你完整的路径。

你可以很容易使用javascript从中提取文件名。

[/编辑]

快乐编码:)


I have used a File upload control in my project.
Now, i want to display Filename selected in Fileupload control in a textbox as soon as user selects a file.
Need quick reply.

解决方案

ok the file upload control has a property called FileName you can retrieve the name of a selceted file
however ,there is another proerty called postedFile this will retrieve an object of type httpPostFile object then it has a mazing porperties as

FileUpload obj;//aleady exist in the web form
//send your page back to server

if(mypage.IspostedBack)
{
if(obj.hasFile)
{httppostedFile obj2=obj.postedFile;
obj2.fileName
obj2.contentType
}
}

and so on..
thank u

[Edit]
Ankur: The code you have given, needs a postback.
[/Edit]


you can use javascript to show selected file by upload controle

<div class="controls">
<asp:fileupload id="filelogo" runat="server" onchange="callme()" xmlns:asp="#unknown">
</asp:fileupload></div>


add textbox here

<pre lang="HTML"><div class="controls">
<asp:FileUpload ID="filelogo" runat="server" onchange="callme(this)">
</div></pre>
<asp:textbox id="txtUploadedFileName" runat="server" xmlns:asp="#unknown"></asp:textbox>



now write the function on .aspx file

<script type="text/javascript">
function callme()
  {
   document.getElementById('<%=txtUploadedFileName.ClientID%>').value=document.getElementById('<%=filelogo.ClientID %>').value;
            
  }

</script></script>



once you will select any file in uploadcontrol name of that file will reflect in text box.


function callme(oFile) {
         document.getElementById("TextBox1").value= oFile.value;
       }





<asp:FileUpload ID="FileUpload1" runat="server"  onchange="callme(this)" />
       <asp:TextBox ID="TextBox1" runat="server"   ></asp:TextBox>



[Edit]
Ankur: But that would give you the full path.
You can easily extract the file name from that using javascript.
[/Edit]
Happy codding :)


这篇关于如何在ASP.Net中更改Fileupload控件时获取Filename?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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