鲨丁鱼.net技术小栈

这里讨论.net的web和form开发,还有其它关于WEB开发和安全的全部知识点,顺带一些经典的有意思的杂文!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

文件上传

Posted on 2006-01-11 14:13  King0502  阅读(102)  评论(0编辑  收藏  举报

  /// <summary>
  ///
  /// </summary>
  /// <returns></returns>
  public static string FileUp(System.Web.UI.HtmlControls.HtmlInputFile file)
  {
   if(file.Value != "")
   {
    int pos = file.Value.Trim().LastIndexOf("\\");
    string picName = file.Value.Trim().Substring(pos + 1);
    pos = picName.LastIndexOf(".");
    string excName = picName.Substring(pos);
    Guid gd = Guid.NewGuid();
    string newName = gd.ToString() + excName;
    //Save in server disk
    file.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("../../images/ProductPic/" + newName));

    return newName;
   }
   else
   {
    return "";
   }
  }