Пример #1
0
        public ActionResult UploadFile(HttpPostedFileBase file, string related_Id)
        {
            var data = fileApp.GetList(related_Id);

            if (data.Count > 3)
            {
                return(Error("最多上传四张图片!"));
            }

            Stream stream = file.InputStream;

            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            string base64    = Convert.ToBase64String(bytes);
            string base64Url = "data:" + file.ContentType + ";base64," + base64;

            FileEntity fileEntity = new FileEntity()
            {
                Related_Id  = related_Id,
                FileName    = file.FileName,
                FileSize    = file.ContentLength,
                FileType    = file.ContentType,
                FileContent = base64Url
            };
            string f_Id = fileApp.SaveFile(fileEntity);

            return(Success("操作成功。", f_Id));
        }
Пример #2
0
        public ActionResult UploadFile(HttpPostedFileBase file, string related_Id)
        {
            var data = fileApp.GetList(related_Id);

            if (data.Count > 3)
            {
                return(Error("最多上传四张图片!"));
            }

            //文件也存一份
            var filePath = Server.MapPath(string.Format("~/{0}{1}", "UploadFile\\", related_Id));

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);//创建该文件夹
            }
            file.SaveAs(Path.Combine(filePath, file.FileName));

            Stream stream = file.InputStream;

            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            string base64    = Convert.ToBase64String(bytes);
            string base64Url = "data:" + file.ContentType + ";base64," + base64;

            FileEntity fileEntity = new FileEntity()
            {
                Related_Id  = related_Id,
                FileName    = file.FileName,
                FileSize    = file.ContentLength,
                FileType    = file.ContentType,
                FilePath    = "UploadFile\\" + related_Id + "\\" + file.FileName,
                FileContent = base64Url
            };
            string f_Id = fileApp.SaveFile(fileEntity);

            return(Success("操作成功。", f_Id));
        }