隐藏

HttpPostedFile 二进制文件上传

发布:2014/7/24 0:04:43作者:管理员 来源:本站 浏览次数:1528

private string FileUpLoad(HttpPostedFile file)
    {
        string fileName, fileExtension;//文件名,文件类型
        fileName = System.IO.Path.GetFileName(file.FileName);
        fileExtension = System.IO.Path.GetExtension(fileName).ToLower();     
        int FileLen = file.ContentLength;
        Byte[] FileData = new Byte[FileLen];
        Stream sr = file.InputStream;//创建数据流对象 
        sr.Read(FileData, 0, FileLen);
        sr.Close();
      return HeadPhotoTemp(FileData, savepath,  fileExtension);
}
    public string HeadPhotoTemp(Byte[] FileByteArray, string savpath, string houzui)
    {
        string filepath = System.Configuration.ConfigurationManager.AppSettings["filepath"].ToString();
        string str = "";
        try
        {
            string path = filepath + savpath;      
            if (!Directory.Exists(Server.MapPath(path)))
            {
                Directory.CreateDirectory(Server.MapPath(path));
            }
            string name = System.DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + DateTime.Now.Ticks + houzui;// ".jpg";
            string savepath = System.Web.HttpContext.Current.Request.MapPath(path) + "/" + name;
            FileStream fs = new FileStream(savepath, FileMode.OpenOrCreate);
            fs.Write(FileByteArray, 0, FileByteArray.Length);
            fs.Close();
            str = path + "/" + name;
        }
        catch (Exception ex)
        {

        }
        return str;
    }