Esempio n. 1
0
        public ActionResult btnUpload_Click(HttpPostedFileBase uploadFile, FormCollection values)
        {
            if (uploadFile != null)
            {
                string fileName = uploadFile.FileName;
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                string truthFileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                uploadFile.SaveAs(Server.MapPath("~/upload/" + truthFileName));

                //保存文件
                INFO_FILEINFO info = new INFO_FILEINFO();
                info.FILENAME   = fileName;
                info.FILEURL    = truthFileName;
                info.UPLOADTIME = DateTime.Now;

                if (Session[WebConstants.UserSession] == null)
                {
                    info.UPLOADUSER = -1;
                }
                else
                {
                    UserInfo user = Session[WebConstants.UserSession] as UserInfo;
                    info.UPLOADUSER = user.Id;
                }
                if (System.IO.File.Exists(Server.MapPath("~/upload/" + truthFileName)))
                {
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath("~/upload/" + truthFileName));
                    info.FILESIZE = System.Math.Ceiling(fileInfo.Length / 1024.0) + "KB";
                }

                var isSuccess = FileBLL.Add(info);

                if (isSuccess)
                {
                    ShowNotify("数据上传成功!");
                }
                else
                {
                    ShowNotify("数据上传失败!");
                }
            }

            // 调用父页面定义的函数 reload
            PageContext.RegisterStartupScript("reload();");
            return(UIHelper.Result());
        }
Esempio n. 2
0
        public ActionResult Upload(String FolderId)
        {
            User user = Session["User"] as User;

            if (user == null)
            {
                return(new RedirectResult(Url.Action("Index", "Error")));
            }

            if (Request.Files.Count > 0)
            {
                Console.Write(Request.Files[0].GetType());
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var    file    = Request.Files[i] as HttpPostedFileWrapper;
                    var    sha1    = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    var    md5byte = sha1.ComputeHash(file.InputStream);
                    String MD5     = Convert.ToBase64String(md5byte);
                    MD5 = Hash_MD5_16(MD5);

                    int folderId = Convert.ToInt32(FolderId);
                    int userId   = user.Id;
                    if (folderId > 0)
                    {
                        userId = 1;
                    }
                    else
                    {
                        folderId = 1;
                    }

                    var fileContent = FileContentBLL.GetByMD5(MD5);
                    if (fileContent != null)
                    {
                        var File = new File()
                        {
                            FileContentId = fileContent.Id, FolderId = folderId, UserId = userId, Name = file.FileName, Size = file.ContentLength.ToString(), Type = file.ContentType, UpdateTime = DateTime.Now.ToShortDateString()
                        };
                        File value = FileBLL.Add(File);
                        fileContent.Data.Close();
                        ResponseHelper.WriteObject(Response, new { Result = true, File = value });
                    }
                    else
                    {
                        file.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "/file/" + MD5);

                        var content = new FileContent()
                        {
                            Data = file.InputStream, MD5 = MD5
                        };

                        content = FileContentBLL.Add(content);

                        var File = new File()
                        {
                            FileContentId = content.Id, FolderId = folderId, UserId = userId, Name = file.FileName, Size = file.ContentLength.ToString(), Type = file.ContentType, UpdateTime = DateTime.Now.ToShortDateString()
                        };
                        var value = FileBLL.Add(File);

                        content.Data.Close();

                        ResponseHelper.WriteObject(Response, new { Result = true, File = value });
                    }
                }
            }

            return(null);
        }