您的位置:首页 > 其它

FileUpload上传文件时,为何不能获取到完整路径

2012-03-23 09:40 253 查看
以前我用FileUpload.PostFile.Filename就可以获取了,但是现在发现不行了,查了很多资料,最后谜底揭开了原来是我IE浏览器的安全性太高了,在工具-->Inernet-->安全-->自定义级别-->启用 “将文件上载到服务器并包含本地路径”OK...
获取不到客户端的路径
HttpContext.Current.Server.MapPath(服务器路径)   这会返回一个服务器的完整路径
FileUpload1.SaveAs(一个字符串,指定服务器上用于保存上载文件的位置的完整路径。)
服务器端是不能获取的。
你可以利用客户端的JS + Hidden Field ,间接获取
FileUpload1.PostedFile.FileName
JavaScript获取FileUpload上传文件的全路径
//函数功能,获取FileUpload上传文件的全路径
function getFullPath(obj)
{
if(obj)
{
//ie
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
obj.select();
return document.selection.createRange().text;
}
//firefox
else if(window.navigator.userAgent.indexOf("Firefox")>=1)
{
if(obj.files)
{
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;
}
}
 string path = Server.MapPath(FileUpload1.FileName);path就是全路径。[/code]
客户端文件路径:          PostedFile.FileName;
文件名称:               FileInfo   file   =   new   FileInfo(name);
string   fileName   =   file.Name;
服务器端文件路径:Server.MapPath( " ");
IQueryable<Customers> tab1 = Customers;List<string> myListOfStrings = new List<string> {"ALFKI", "ANATR", "ANTON"};var predicate = PredicateBuilder.False<Customers>();foreach (String m in myListOfStrings){string n = m;predicate = predicate.Or(c => c.CustomerID.Contains(n));}tab1 = tab1.Where(predicate);
The PredicateBuilder's source code is available and can be included in your application.Using PredicateBuilder:http://www.albahari.com/nutshell/predicatebuilder.aspx MarcelProposed As Answer by KristoferA - Huagati SystemsEditor Monday, August 02, 2010 5:36 AMMarked As Answer by Alex LiangModerator Thursday, August 05, 2010 3:18 AMYou can follow Marcel’s suggestion, use PredicateBuilder to implement what you need. Here is a complete example based on NorthWind database.
private void button1_Click(object sender, EventArgs e){using (NorthWindDataContext ctx = new NorthWindDataContext()){List<string> myListOfStrings = new List<string> { "fish", "Seaweed" };var predicate = PredicateBuilder.False<Category>();foreach (string keyword in myListOfStrings){string temp = keyword;predicate = predicate.Or(p => p.Description.Contains(temp));}IQueryable<Category> query = ctx.Categories.Where(predicate);//You can check the Commmand Text hereConsole.WriteLine(ctx.GetCommand(query).CommandText);this.dataGridView1.DataSource = query;}}
Below is the PredicateBuilder class.
public static class PredicateBuilder{public static Expression<Func<T, bool>> True<T>() { return f => true; }public static Expression<Func<T, bool>> False<T>() { return f => false; }public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,Expression<Func<T, bool>> expr2){var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);}public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,Expression<Func<T, bool>> expr2){var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);}}
If you have other question, please feel free to let me know. Best regards,Alex LiangMSDN Subscriber Support in ForumIf you have any feedback on our support, please contact msdnmg@microsoft.comPlease remember to mark the replies as answers if they help and unmark them if they provide no help.Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: