.net文件上传下载

2021-04-23
这个写法是基于HUI前端框架的JS进行处理的。否则的话 var file = $("#FileUp").val();在谷歌浏览器得到的路径是不对的。

文件上传前台:前台主要就是用file控件,Form表达Post文件流到后台


    <form id="FileUpload" action="/File/Upload/" method="post">

        <div class="row cl mt-10 ml-10"> 

            <a>

                <input id="FileUp" class="file" runat="server" name="file" type="file" /></a>

            <a class="btn btn-success" οnclick="FileUpload()"><i class="Hui-iconfont">&#xe600;</i>&nbsp;上传</a>

        </div>

    </form>


   function FileUpload() {

            var file = $("#FileUp").val();

            if (typeof (file) == undefined || file == "") {

                layer.msg("请选择文件!", { icon: 2, time: 2000 });

                return false;

            }

            $("#FileUpload").ajaxSubmit(function (json) {

                if (json.statusCode == 200) {

                   layer.msg("上传成功", { icon: 1, time: 2000 });

                    location.reload();

                } else {

                   layer.msg(json.message, { icon: 2, time: 2000 });

                }

            });

        }

文件上传后台,后台接收文件流


[HttpPost]


        public JsonResult Upload(object sender, EventArgs e, int Parentid)

        {

            JsonResult result = new JsonResult();

            string FilePath = "";//ConfigurationManager.AppSettings["FilePath"];            

            try

            {

                for (int i = 0; i < Request.Files.Count; i++)

                {

                    if (!string.IsNullOrEmpty(Request.Files[i].FileName))

                    {

                        string filename = Path.GetFileName(Request.Files[i].FileName);

                        FilePath = Path.Combine(Server.MapPath("/") + "\\tempFiles\\Upload", Path.GetFileName(Request.Files[i].FileName));//文件存储路径

                        Request.Files[i].SaveAs(FilePath);//保存文件

                        string[] nametype = filename.Split('.');

                        string msg = "";

                        FilePath = FilePath.Replace("\\", "\\\\");

                        nametype[1] = Server.UrlEncode(".") + Server.UrlEncode(nametype[1]);

                        if (_Bfile.InsertFileUpload(nametype[0], Employee.Id, Parentid, nametype[1], FilePath, out msg) > 0)//存储数据库

                            result.Data = new { statusCode = 200, message = "上传成功" };

                        else

                            result.Data = new { statusCode = 300, message = msg };

                    }

                }

            }

            catch (Exception ex)

            {

                result.Data = new { statusCode = 300, message = "上传异常" };

            }

            return result;

        }



文件下载前端:


 <a href="/File/Download?FileID=@(dr["Id"])&FileURl=@Html.Raw(dr["FileURl"])&FileName=@(dr["FileName"])&Format=@(dr["FileFormat"])" title="下载" 

                                @Html.Raw(dr["FileFormat"].ToString() == "folder" ? "style=\"display:none\"" : "")>下载 </a>

文件下载后台:


        public ActionResult Download(int FileID, string FileURl, string FileName, string Format)

        {           

            FileStream fs = new FileStream(FileURl, FileMode.Open);//打开文件所在路径,获取文件流

            byte[] bytes = new byte[(int)fs.Length];

            fs.Read(bytes, 0, bytes.Length);

            fs.Close();

            Response.Charset = "UTF-8";

            Response.ContentEncoding = Encoding.GetEncoding("UTF-8");//将文件流按UTF8编码

            Response.ContentType = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(FileName) + Server.UrlEncode(Format));

            Response.BinaryWrite(bytes);

            Response.Flush();//在网页输出文件流

            Response.End();

            //设置基本信息   

            _Bfile.InsertFileDownload(FileID, Employee.Id);             

            return new EmptyResult();

        }