日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

MVC 上传文件

發布時間:2024/9/5 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVC 上传文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
if (file != null){string path = Server.MapPath("~/Uploads/"); //獲得服務器物理路徑if (!System.IO.Directory.Exists(path))//判斷目錄是否存在 {System.IO.Directory.CreateDirectory(path);//創建目錄 }file.SaveAs(path + System.IO.Path.GetFileName(file.FileName)); //獲得文件名和擴展名} if (file.ContentLength > 0)//獲取上傳文件的大小,以字節為單位 {string FileName = System.IO.Path.GetFileName(file.FileName);//返回文件名+擴展名string path = System.IO.Path.Combine(Server.MapPath("/bin"), file.FileName); //將兩個字符串組合成一個路徑 file.SaveAs(path);}

?

[HttpPost]public ActionResult Index(IEnumerable<HttpPostedFileBase> files) //接收多個文件 {foreach (var file in files){if (file.ContentLength > 0){var fileName = System.IO.Path.GetFileName(file.FileName);var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);file.SaveAs(path);}}return RedirectToAction("Index");}

?

<div> @*上傳單個文件*@<form action="/Home/UpdateFile2" enctype="multipart/form-data" method="post" id="form1"><input type="file" name="file" /><!--只能選擇單個文件---><input type="button" name="Submit" id="Submit" value="Upload" /></form> @*上傳多個文件*@<form action="/Home/UpdateFile1" enctype="multipart/form-data" method="post"><input type="file" name="img" multiple="multiple" /> <!--可選擇多個文件--></form></div>

?

public ActionResult Upload(HttpPostedFileBase[] fileToUpload){foreach (HttpPostedFileBase file in fileToUpload){string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName));file.SaveAs(path);}ViewBag.Message = "File(s) uploaded successfully";return RedirectToAction("Index");}

?

[HttpPost]public bool UploadFiles(){// 文件數為0證明上傳不成功if (Request.Files.Count == 0){throw new Exception("請選擇上傳文件!");}string uploadPath = Server.MapPath("../UploadFiles/");// 如果UploadFiles文件夾不存在則先創建if (!System.IO.Directory.Exists(uploadPath)){System.IO.Directory.CreateDirectory(uploadPath);}// 保存文件到UploadFiles文件夾for (int i = 0; i < Request.Files.Count; ++i){HttpPostedFileBase file = Request.Files[i];// 文件名為空證明沒有選擇上傳文件if (file.FileName == ""){return false;}string filePath = uploadPath + System.IO.Path.GetFileName(file.FileName);string fileName = file.FileName;// 檢查上傳文件的類型是否合法string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();string fileFilter = System.Configuration.ConfigurationManager.AppSettings["FileFilter"];if (fileFilter.IndexOf(fileExtension) <= -1){Response.Write("對不起!請上傳圖片!!");return false;}// 如果服務器上已經存在該文件則要修改文件名與其儲存路徑while (System.IO.File.Exists(filePath)){Random rand = new Random();fileName = rand.Next().ToString() + "-" + file.FileName;filePath = uploadPath + System.IO.Path.GetFileName(fileName);}// 把文件的存儲路徑保存起來//SaveUploadFileInfo(fileName, filePath);// 保存文件到服務器 file.SaveAs(filePath);}return true;}

?

<appSettings>...<add key="FileFilter" value=".gif|.jpg|.jpeg|.png|" />...</appSettings>

?

  

轉載于:https://www.cnblogs.com/enych/p/8351388.html

總結

以上是生活随笔為你收集整理的MVC 上传文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。