public ActionResult DownloadFiles(int Id) { var client = new SysModuleServiceReference.SysModuleServiceClient(); FilesDTO files = client.GetFiles(Id); // 根据id查找文件名 内容字节数组 GZip zip = new GZip(); // 解压包类 files.FileContent = zip.DecompressStream(files.FileContent); // 解压上传文件内容 Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Url.Encode(files.FileName)); Response.BinaryWrite(files.FileContent); Response.Flush(); Response.End(); return new EmptyResult(); }
以文件字节数组下载文件
public ActionResult UploadPic(HttpPostedFileBase fileData) { var client = new SysModuleServiceReference.SysModuleServiceClient(); FilesDTO files = new FilesDTO(); GZip zip = new GZip(); // 解压包类 string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称 byte[] buffer = new byte[fileData.InputStream.Length + 1]; // 上传文件字节数组大小 fileData.InputStream.Read(buffer, 0, buffer.Length); // 把上传文件的大小放入 buffer里 buffer = zip.CompressStream(buffer); // 为了节省空间 压缩文件 files.FileExtName = Path.GetExtension(fileName); // 文件扩展名 files.FileContent = buffer; files.FileName = fileName; files.FileTypeValue = int.Parse(Request["FileTypeValue"]); files.FKId = int.Parse(Request["FKId"]); files.InPerId = this.CustomContext.UserID; files.InTime = DateTime.Now; client.AddFiles(files); //上传服务器 return Json(new { Success = true, FileName = fileName, SaveName = fileName }); // 返回成功 }
以文件字节数组上传文件