文件的上傳與下載


一、文件上傳

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();
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章