zoukankan      html  css  js  c++  java
  • c#代码文件上传和下载

      public JsonResult UploadFile(DriverFileManager filem)
            {
               
                var hfc = System.Web.HttpContext.Current.Request.Files;

                var filePathRoot = GetAppSetting.GetPhysicalPath + GetFileType(filem.FileType ) + "\";
                //判断是否存在目录
                if (!Directory.Exists(filePathRoot))
                {
                    Directory.CreateDirectory(filePathRoot);
                }
                if (hfc.Count > 0 && !string.IsNullOrEmpty(hfc[0].FileName)) {
                    var filename = hfc[0].FileName;
                    var filePath = filePathRoot + Path.GetFileName(filename);

                    //判断是否存在文件名
                    if (System.IO.File.Exists(filePath))
                    {
                        //重命名新文件
                        filename = Path.GetFileNameWithoutExtension(filename) + "_" + new Random().Next(0, 1000) + Path.GetExtension(filename);
                    }
                    else
                    {
                        filename = Path.GetFileName(filename);
                    }
                    filePath = filePathRoot + filename;
                    hfc[0].SaveAs(filePath);
                    filem.FilePath = filename;
                    filem.CreateBy = CurrentUserInfo.UserCode;
                    var obj = DriverFileManager.Save(filem);
                  
                      return Json(new { err = "", msg = "导入成功!" }, "text/html");
                      
                }

                return null;
            }

    /////////////////////////////////////////////////////////////////////////下载//////////////////////////////////////////////////////////////////////////////////////

    using Cinway.Common;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace Cinway.Controllers
    {
        public class FileDownsController : Controller
        {
             //
            // GET: /FileDowns/
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>
            public ActionResult DowmFile(string filePath, string FileType)
            {
                string fileName=filePath;
                filePath = GetAppSetting.GetPhysicalPath + FileType + "/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }

            //
            // GET: /FileDowns/
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>
            public ActionResult DowmFileReceipt(string filePath, string FileType)
            {
                string fileName = filePath;
                filePath = GetAppSetting.GetPhysicalPath + "Receipt" + "/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }

            //
            // GET: /FileDowns/
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>
            public ActionResult ReceiptDowmFile(string filePath, string FileType)
            {
                string fileName = filePath;
                filePath = GetAppSetting.GetPhysicalPath + "Receipt/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }
            public ActionResult PreviewDowmFile(string filePath, string FileType)
            {
                string fileName = filePath;
                filePath = GetAppSetting.GetPhysicalPath + "Receipt/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                if (filePath.Contains(".pdf"))
                {
                    Response.ContentType = "application/pdf";
                }
                else if(filePath.Contains(".jpg"))
                {
                    Response.ContentType = "image/jpg";
                }
                else if(filePath.Contains(".png"))
                {
                    Response.ContentType = "image/png";
                }
                else
                {
                    Response.ContentType = "application/octet-stream";
                }


                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "inline;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                //Response.Flush();
                // Response.End();
                try { Response.Flush(); }
                catch { }
                finally
                {
                    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                //Response.Close();
                return new EmptyResult();
            }
            //
            // GET: /FileDowns/
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>
            public ActionResult DowmFile1(string filePath, string FileType)
            {
                string fileName = filePath;
                filePath = GetAppSetting.GetPhysicalPath +"TruckOrder/"+ FileType + "/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }



            //
            // GET: /FileDowns/
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>
            public ActionResult DowmFiles(string filePath, string FileType)
            {
                string fileName = filePath;
                int intfiletype = 0;
                if (int.TryParse(FileType, out intfiletype))
                {
                    FileType = TruckFileManagerController.GetFileType(intfiletype);
                }

                filePath = GetAppSetting.GetPhysicalPath + FileType + "/" + filePath;

                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }


            #region 下载模板,关务
            public ActionResult DownModal(string fileName) {
                string filePath = Server.MapPath("~/ExcelModel/" + fileName+".xlsx");
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName + ".xlsx") : fileName + ".xlsx"));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();
                return new EmptyResult();
            }
            #endregion
            #region 洪祺 订单上传附件下载
            public ActionResult DowmOrderFiles(string filePath, string FileType,string fileName)
            {
                int intfiletype = 0;
                if (int.TryParse(FileType, out intfiletype))
                {
                    FileType = TruckFileManagerController.GetFileType(intfiletype);
                }
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);

                fs.Close();

                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.Expires = 0;
                Response.Buffer = true;
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.ContentType = "application/octet-stream";

                //解决IE中文乱码问题
                bool ie = Request.Headers.Get("User-Agent").IndexOf("MSIE") > 0 || Request.Headers.Get("User-Agent").IndexOf("Trident") > 0;

                Response.AddHeader("Content-Disposition", "attachment;filename=" + (ie ? Server.UrlEncode(fileName) : fileName));
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);

                Response.Flush();
                Response.End();

                return new EmptyResult();
            }
            #endregion
        }
    }

  • 相关阅读:
    H5及微信中唤起app的解决方案
    html5统计数据上报API:SendBeacon
    基于webpack4的react开发环境配置
    electron-vue开发爬坑指南
    利用git 进行多人协作开发
    js 性能优化利器:prepack
    各种渲染方式对比解析
    Nuxt.js部署应用的方式
    微信小程序--data的赋值与取值
    甘超波:什么是个人定位
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/7606315.html
Copyright © 2011-2022 走看看