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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET-文件上传代码

發布時間:2023/12/18 asp.net 29 如意码农
生活随笔 收集整理的這篇文章主要介紹了 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-文件上传代码的全部內容,希望文章能夠幫你解決所遇到的問題。

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