Example #1
0
        /// <summary>
        /// 保存上传的文件
        /// </summary>
        /// <param name="postFileBase"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private File SaveHttpPostFile(HttpPostedFileBase postFileBase,string path)
        {
            System.IO.Stream uploadStream = null;
            System.IO.FileStream fs = null;

            File file = new File();
            try
            {
                uploadStream = postFileBase.InputStream;
                int bufferLen = 1024;
                byte[] buffer = new byte[bufferLen];
                int contentLen = 0;

                file.Name = System.IO.Path.GetFileName(postFileBase.FileName);
                file.ContentType = postFileBase.ContentType;
                file.RealPath = path + Guid.NewGuid().ToString();
                fs = new System.IO.FileStream(file.RealPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);

                while ((contentLen = uploadStream.Read(buffer, 0, bufferLen)) != 0)
                {
                    fs.Write(buffer, 0, bufferLen);
                    fs.Flush();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (null != fs)
                {
                    fs.Close();
                }
                if (null != uploadStream)
                {
                    uploadStream.Close();
                }
            }
            file.Size = GetFileSize(file.RealPath);
            file.Md5 = Md5Util.GetMD5HashFromFile(file.RealPath);

            return file;
        }
Example #2
0
        public ActionResult Upload()
        {
            IList<File> retVal = new List<File>();

            Url = Server.MapPath("/") + @"Files\";

            System.IO.Stream uploadStream = null;
            System.IO.FileStream fs = null;
            //文件上传,一次上传1M的数据,防止出现大文件无法上传
            HttpFileCollectionBase files = Request.Files;
            for (int i = 0; i < files.Count;i++ )
            {
                File file = new File();
                try
                {
                    HttpPostedFileBase postFileBase = files[i];
                    uploadStream = postFileBase.InputStream;
                    int bufferLen = 1024;
                    byte[] buffer = new byte[bufferLen];
                    int contentLen = 0;

                    file.Name = System.IO.Path.GetFileName(postFileBase.FileName);
                    file.ContentType = postFileBase.ContentType;
                    file.RealPath = Url + Guid.NewGuid().ToString();
                    fs = new System.IO.FileStream(file.RealPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);

                    while ((contentLen = uploadStream.Read(buffer, 0, bufferLen)) != 0)
                    {
                        fs.Write(buffer, 0, bufferLen);
                        fs.Flush();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (null != fs)
                    {
                        fs.Close();
                    }
                    if (null != uploadStream)
                    {
                        uploadStream.Close();
                    }
                }

                file.Size = GetFileSize(file.RealPath);
                file.Md5 = Md5Util.GetMD5HashFromFile(file.RealPath);

                //查找是否存在过相同MD5值的文件,如果是则不保存到数据库
                IList<File> searchFiles=fileOper.Get(
                    delegate(object sender, ICriteria criteria)
                    {
                        criteria.Add(Restrictions.Eq("Md5", file.Md5));
                    }
                    );

                if (searchFiles.Count > 0)
                {
                    //删除上传的文件并把数据库相同文件信息返回
                    DelFile(file.RealPath);
                    file = searchFiles[0];
                }
                else
                {
                    //保存文件到数据库
                    fileOper.Add(file);
                }

                retVal.Add(file);
            }

            return Json(retVal);
        }
Example #3
0
 public ActionResult Del(File file)
 {
     return Json(fileOper.Del(file));
 }
Example #4
0
 public ActionResult Update(File file)
 {
     return Json(fileOper.Update(file));
 }
Example #5
0
        //public void DownLoad(string id)
        //{
        //    File file=fileOper.Get(new Guid(id));
        //    if (file != null)
        //    {
        //        Response.ClearHeaders();
        //        Response.Clear();
        //        Response.Expires = 0;
        //        Response.Buffer = true;
        //        Response.AddHeader("Accept-Language", "zh-tw");
        //        System.IO.FileStream files = new System.IO.FileStream(file.RealPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
        //        byte[] byteFile = null;
        //        if (files.Length == 0)
        //        {
        //            byteFile = new byte[1];
        //        }
        //        else
        //        {
        //            byteFile = new byte[files.Length];
        //        }
        //        files.Read(byteFile, 0, (int)byteFile.Length);
        //        files.Close();

        //        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
        //        Response.ContentType = "application/octet-stream;charset=utf-8";
        //        Response.BinaryWrite(byteFile);
        //        Response.End();
        //    }
        //}

        public ActionResult Add(File file)
        {
            return Json(fileOper.Add(file));
        }