20160620001 FileUpload控件获取上传文件的路径
参考地址: http://bbs.csdn.net/topics/350051517
——————————————————————————————
用js实现
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BorrowPage.aspx.cs" Inherits="Borrow_BorrowPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>xxx上传</
title
>
<!--获取文件上传地址-->
<
script
type
=
"text/javascript"
>
function checkFile()
{
//判断浏览器类型
var isIE = (document.all) ? true : false;
var isIE7 = isIE && (navigator.userAgent.indexOf('MSIE 7.0') != -1);
var isIE8 = isIE && (navigator.userAgent.indexOf('MSIE 8.0') != -1);
var file=document.getElementById("UpLoadBorrow");
var path=file.value;
if(isIE7 || isIE8)
{
file.select();
path=document.selection.createRange().text;
document.selection.empty();
}
document.getElementById("txtFilePath").value=path;
}
function checkNull()
{
var path=document.getElementById("txtFilePath").value;
if(path=="" ||path==null)
{
alert('请选择要上传的文件!');
return false;
}
return true;
}
</
script
>
<
base
target
=
"_self"
/>
</
head
>
<
body
style
=
"background-color:#0099FF;"
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
style
=
"margin:0px auto 0px auto;"
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
BorderStyle
=
"Double"
Width
=
"750"
ImageAlign
=
"Middle"
AlternateText
=
"上传"
/>
<
br
/>
上传:<
asp:FileUpload
ID
=
"UpLoadBorrow"
runat
=
"server"
Height
=
"22px"
onchange
=
"checkFile()"
onkeydown
=
"event.returnValue=false;"
onpaste
=
"return false"
/>
<
input
type
=
"hidden"
id
=
"txtFilePath"
runat
=
"server"
/>//这里建立一个隐藏域 用来存地址
<
asp:Button
ID
=
"btnUpload"
runat
=
"server"
Text
=
"上传"
OnClientClick
=
"return checkNull()"
OnClick
=
"btnUpLoad_Click"
/>
<
asp:Button
ID
=
"btnBack"
runat
=
"server"
Text
=
"返回"
onmousedown
=
"window.close();"
/></
div
>
</
form
>
</
body
>
</
html
>
——————————————————————————————
后台调用:
string
inputPath = txtFilePath.Value.Trim();
——————————————————————————————
——————————————————————————————
fileupload 有安全机制 无法获得服务器路径。
只能通过JS或者修改部分浏览器设置来得到客户端路径。
——————————————————————————————