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.
相关文章推荐
- FileUpload上传文件时,为何不能获取到完整路径
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法
- JS、JSP不能正确获取上传文件路径问题
- asp.net中FileUpload得到上传文件的完整路径
- 获取FileUpload中的上传文件路径
- 20160620001 FileUpload控件获取上传文件的路径
- 关于js获取上传文件的完整路径问题?
- FileUpload 控件上传文件路径获取不到。
- FileUpload控件获取要上传文件的路径
- 上传时获取文件的完整路径图片预览的js代码(兼容Firfox和IE)
- (原创)js提交文件,js上传文件,纯js解决无刷新文件上传,不使用form提交文件,并获取返回值(路径url)
- 浅谈FireFox中file控件不能取到客户端文件的完整路径的问题 (转)
- 浅谈FireFox中file控件不能取到客户端文件的完整路径的问题
- javascript 怎么获取文件上传对话框中的全路径名
- 上传文件 获取路径
- 解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题
- flex中.mxml中获取上传文件的路径,绝对不是js回调函数
- FireFox中file控件不能取到客户端文件的完整路径的问题(2012.11.27)
- CSS美化上传按钮并获取上传文件路径
- JS 获取IE上传文件路径(IE7,8)