ASP.NET-文件上传代码
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET-文件上传代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
|
1 2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public ActionResult upload() { ViewBag.ImageUrl = " "; return View(); } // 模型綁定ModelBinding MVC 框架幫你抽取Request值,轉換成視圖模型對象 [HttpPost] public ActionResult upload(List<HttpPostedFileBase> files) { if (files != null) { List<string> urls = new List<string>(); foreach (var file in files) { if (file != null && file.ContentLength > 0) { // 1.獲取文件名 string fileName=Path.GetFileName(file.FileName); string timeName = DateTime.Now.Ticks.ToString(); // 2.保存文件到網站文件夾 string localpath=Server.MapPath("~/UploadFile/"); string tempName = timeName + fileName; string fullpath = localpath + tempName; file.SaveAs(fullpath); // 傳遞到視圖圖片路徑,顯示圖片 string temp = "/UploadFile/" + tempName; urls.Add(temp); //ViewBag.ImageUrl = fullpath; // 3.保存的路徑存入數據庫的地址字段 } } ViewBag.urls = urls; } return View(); } |
后臺代碼主要使用HttpPostedFileBase對象來操作file這個流對象
前臺主要使用form表單工具來提交文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@using (Html.BeginForm("upload", "Order", FormMethod.Post, new { enctype = "multipart/form-data" })){ <input type="file" id="files" name="files" multiple="multiple"/> <input type="submit" value="上傳圖片" class="btn btn-default form-control" />}@if (ViewBag.urls != null){ foreach (var url in ViewBag.urls) { <img style="width:300px;" src="@url" /> }} |
來自為知筆記(Wiz)
總結
以上是生活随笔為你收集整理的ASP.NET-文件上传代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 挤出滚圆机如何实现高效率、低能耗的生产?
- 下一篇: asp.net ajax控件工具集 Au