文件的上传与下载

来源:互联网 发布:js如何遍历数组 编辑:程序博客网 时间:2024/05/21 17:40


一、文件上传

1、前端

 <div class="motherdiv"  style="margin:0px;width:100% !important;padding:0 ">
                            <form action="/Announcement/UploadFile" method="post" enctype="multipart/form-data" target="framFile" style="width:100%">
                                <!---input type="file" name="file" /><br /-->
                                <!--a-- class="btn" href="/WorkFlow/MyFormData/ExcelFileTemplet?FileName=' + item.control_label + '" style="background-color: #1ABC9C;border-color: #1ABC9C;color: white;float:right !important">下载模板</!--a-->
                                <input type="file" name="FileUpload1" class="fileupload form-control" style="background-color:white;width:50%;float:left;border-color:white !important" />
                                <input type="submit" name="Submit" id="Submit" value="上传数据" style="padding: 2px;margin: 2px;float:right" />
                                <iframe id="framFile" name="framFile" style="display:none;"></iframe>
                            </form>
                        </div>

2、后台

public void UploadFile(FormCollection form)
        {
            if (Request.Files.Count == 0)
            {
                //Request.Files.Count 文件数为0上传不成功
                //return Content("<script>alert('上传文件为空...')</script>", "text/html");
            }
            var file = Request.Files[0];
            if (file.ContentLength == 0)
            {
                //文件大小大(以字节为单位)为0时,做一些操作
                //return Content("<script>alert('上传文件无数据...')</script>", "text/html");
            }
            else
            {
                //文件大小不为0
                file = Request.Files[0];
                //保存成自己的文件全路径,newfile就是你上传后保存的文件,
                //服务器上的UpLoadFile文件夹必须有读写权限
                string Url = Server.MapPath("~/File/");//取得目标文件夹的路径
                if (!Directory.Exists(Url))
                {
                    // 创建up文件夹  
                    Directory.CreateDirectory(Url);
                }
                string filename = file.FileName;//取得文件名字
                string path = Url + filename;//获取存储的目标地址
                file.SaveAs(path);
            }
            //return Content("<script>alert('上传成功...')</script>", "text/html");
        }

二、文件的下载

1、前端

<a class="btn" id="bt_Download" href="/Announcement/LoadFile" style="background-color: #1ABC9C;border-color: #1ABC9C;color: white;">文件下载</a>

2、后台

public void LoadFile()
        {
            //string fileName = FileName + ".xls";//客户端保存的文件名
            //var filePath = Server.MapPath(string.Format("~/{0}", "File/android-debug.apk"));
            //return new FilePathResult(filePath, "application/vnd.android");
            //string fileName = "a.jpg";//客户端保存的文件名   
            string fileName = "android-debug.apk";
            string filePath = Server.MapPath("~/File/");//路径


            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath+ fileName, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

原创粉丝点击