FileUpload 获取全路径的问题

tkscascor 2009-09-24 08:50:20
我使用的是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"
...全文
1495 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tlmiai 2009-11-10
  • 打赏
  • 举报
回复
在安全设置里面设置一下就OK了
游北亮 2009-09-24
  • 打赏
  • 举报
回复
应该是:ie7 ie8 已经不支持用js获取全路径了。

我刚刚在另一台机器的VS.Net2003下做了测试,
结果和2楼是一致的

说明应该是你哪里出了问题,而不是VS2003和VS2008不同
tkscascor 2009-09-24
  • 打赏
  • 举报
回复
4楼 还在么. 给我解释下行不,
nosuchtracter 2009-09-24
  • 打赏
  • 举报
回复
记得用Html的file控件可以获取的,加个runat="server"
tkscascor 2009-09-24
  • 打赏
  • 举报
回复
OK
4#
gongsun 2009-09-24
  • 打赏
  • 举报
回复
var thehid=document.getElementById("hidthepath");

js中 这一句你也的改下 hidthepath 就是那个 隐藏字段
gongsun 2009-09-24
  • 打赏
  • 举报
回复
当你的 文件控件 onchange时 触发GetThePath()
sdnjwang 2009-09-24
  • 打赏
  • 举报
回复

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 = "没有图片上传";
}
liujintaozyc 2009-09-24
  • 打赏
  • 举报
回复
先把file加个runat=server转化为服务器控件
然后postedfile.filename就可以得到全部的路径
自己试试 我以前用的就是这
gongsun 2009-09-24
  • 打赏
  • 举报
回复
ie7 ie8 已经不支持获取全路径了。

你可以加个隐藏字段存值。


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);
}
tkscascor 2009-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 youbl 的回复:]
    <input type="file" id="aa" runat="server" />
    <asp:Button ID="btn1" runat="server" Text="test" />

提交后,在后台用:
aa.PostedFile.FileName得到的是:
C:\\Documents and Settings\\Administrator\\桌面\\url.txt
带路径的,不过我的环境是VS2008
[/Quote]
貌似03的不行. 只获取到url.txt 后面这个
游北亮 2009-09-24
  • 打赏
  • 举报
回复
<input type="file" id="aa" runat="server" />
<asp:Button ID="btn1" runat="server" Text="test" />

提交后,在后台用:
aa.PostedFile.FileName得到的是:
C:\\Documents and Settings\\Administrator\\桌面\\url.txt
带路径的,不过我的环境是VS2008
wenwenlou 2009-09-24
  • 打赏
  • 举报
回复
<input type="file" runat="server">这样试试看行不~~我记得可以的
<asp/>这样的是得到最后的名称,

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧