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

以前我用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;
            }
        }
[code=C#][/code] string path = Server.MapPath(FileUpload1.FileName);
path就是全路径。
客户端文件路径:          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

 

Marcel

You 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 here 
    Console.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 Liang

MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please 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. 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值