关闭

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

标签: string服务器queryjavascriptfirefoxpath
557人阅读 评论(0) 收藏 举报
分类:

以前我用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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:125064次
    • 积分:1552
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:208篇
    • 译文:0篇
    • 评论:6条