我使用的是vs 2003 . asp.net
file 上传控件 需要获取到全路径, 而不是仅仅获取文件名. 比如file上面显示了 c:\temp.txt
我使用value属性,postfile.Filename 属性, 都是获取到temp.txt,
谁有解决办法. 不管重写value属性,还是使用javascript控制获取.
只要有解决办法.解决了,马上结贴
PS.
这种方式已经测试过,无法使用
onpropertychange="javascript:alert(this.value);document.getElementById('hd_Path').value=this.value" 因为使用这种方法, 会默认this.value="C:\fakepath\temp.txt"
file 上传控件 需要获取到全路径, 而不是仅仅获取文件名. 比如file上面显示了 c:\temp.txt
我使用value属性,postfile.Filename 属性, 都是获取到temp.txt,
谁有解决办法. 不管重写value属性,还是使用javascript控制获取.
只要有解决办法.解决了,马上结贴
PS.
这种方式已经测试过,无法使用
onpropertychange="javascript:alert(this.value);document.getElementById('hd_Path').value=this.value" 因为使用这种方法, 会默认this.value="C:\fakepath\temp.txt"
<asp/>这样的是得到最后的名称,
<asp:Button ID="btn1" runat="server" Text="test" />提交后,在后台用:
aa.PostedFile.FileName得到的是:
C:\\Documents and Settings\\Administrator\\桌面\\url.txt
带路径的,不过我的环境是VS2008
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 isMozilla = /mozilla/.test( navigator.userAgent ) && !/(compatible|webkit)/.test( navigator.userAgent );
var isOpera = /Opera/.test( navigator.userAgent );
var thehid=document.getElementById("hidthepath");
if(isIE7 || isIE8){
thehid.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='hidden')"
}
function GetThePath()
{
var thefile=document.getElementById("你的文件控件");
var thepath=thefile.value;
if(isIE7 || isIE8){
thefile.select();
var thepath=document.selection.createRange().text;
document.selection.empty();
}else if(isMozilla){
thepath = thefile.files[0].getAsDataURL();
}
document.getElementById('你的隐藏字段').value=thepath;
//alert(document.getElementById('你的隐藏字段').value);
}
然后postedfile.filename就可以得到全部的路径
自己试试 我以前用的就是这
Boolean fileOK = false;
String path = Server.MapPath("photo");
if (PhotoFul.HasFile) //判断是否有图片传上来了
{
String fileExtension =
System.IO.Path.GetExtension(PhotoFul.FileName).ToLower();//获取文件扩展名
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg", ".bmp" }; //允许上传的文件格式
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
break;
}
} if (fileOK)
{
try
{
PhotoFul.PostedFile.SaveAs(path + "\\" + PhotoFul.FileName);
PhotoImg.Visible = true;
float newwt, newht;
int oldwt = matic.toint(PhotoImg.Width.ToString());
int oldht = matic.toint(PhotoImg.Height.ToString());
matic.ChangPictur(PhotoFul.PostedFile.FileName, out newwt, out newht, oldwt, oldht);
//matic.ChangPictur("photo/" + PhotoFul.FileName, out newwt, out newht, oldwt, oldht);
this.PhotoImg.Width = 322;
this.PhotoImg.Height = 322;
PhotoImg.Height = (int)newht;
PhotoImg.Width = (int)newwt;
PhotoImg.ImageUrl = "photo" + "\\" + PhotoFul.FileName;
PhotoLab.Text = "上传图片成功";
this.PhotoImg.AlternateText = "无法显示图片";
}
catch (Exception ex)
{
Response.Write("<script>PhotoLab.ForeColor = 'Red';</script>");
PhotoLab.Text = "ERROR: " + ex.Message.ToString(); }
finally { }
}
else
{
PhotoLab.Text = "文件的类型不合适";
}
}
else
{
PhotoLab.Text = "没有图片上传";
}
结果和2楼是一致的说明应该是你哪里出了问题,而不是VS2003和VS2008不同