How can I have a control on a ASP.NET form that browses for files as FileUpload
control does, but instead of submmitting the whole file, it only submits the path from witch the file was found from (with the original file name)?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Here is a workaround I made by following the instructions of the following forum post: http://forums.asp.net/p/1189182/2040139.aspx#2040048
<asp:FileUpload ID="File1" runat="server" onchange="GetFileName();"/>
<asp:Button ID="Submit" runat="server" Text="Submit" OnClientClick="DisableFileSelector();" />
<asp:HiddenField id="txtFileName" runat="server" />
<script language="javascript" type="text/javascript">
function GetFileName()
{
document.getElementById('<%=txtFileName.ClientID %>').value = document.getElementById('<%=File1.ClientID %>').value;
}
function DisableFileSelector() {
document.getElementById('<%=File1.ClientID %>').disabled = true;
}
</script>
Of course, this only works if JavaScript is enabled. If the JavaScript is disabled, the form will submit the file, but you can still perserve the functionality of the site by adding the following code before the first time txtFileName
is being use.
if (File1.HasFile)
{
txtFileName.Value = File1.PostedFile.FileName;
}