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

歡迎訪問 生活随笔!

生活随笔

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

asp.net

ASP.NET MVC 上传图片到项目目录中的文件夹并显示

發布時間:2025/3/11 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC 上传图片到项目目录中的文件夹并显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

因項目需求,需要一個上傳圖片并顯示的功能,類似于上傳頭像并顯示出來。查閱了網上資料,寫了個Demo,希望能幫助到更多的人。此Demo基于ASP.NET MVC實現。

?選擇圖片:

點擊按鈕進行上傳:?

一、先在項目中新建一個文件夾用于存放圖片?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

二、View頁面代碼?

@{Layout = null; }<!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0"><script src="~/Scripts/jquery-3.3.1.min.js"></script><script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> </head> <body>@using (Ajax.BeginForm("", null, new AjaxOptions() { OnSuccess = "PostSuc", OnFailure = "PostFail", HttpMethod = "Post" }, new { enctype = "multipart/form-data", id = "FormBaseData" })){<input type="hidden" name="MouldId" id="MouldId" value="9527" /><input type="file" name="file1" /><input type="button" value="submit" id="btnSubmit" /><img src="" width="300" height="300" display:inline; alt="圖片預覽" id="mypicture" />} </body> </html><script type="text/javascript">$(document).ready(function () {$("#btnSubmit").bind("click", function () { Query(); });})function Query() {$("#FormBaseData").attr("data-ajax-success", "OnQuerySuccess(data)");$("#FormBaseData").attr("data-ajax-failure", "OnQueryFail()");$("#FormBaseData").attr("action", "/home/UploadImg");$("#FormBaseData").submit();}function OnQuerySuccess(data) {$("#mypicture").attr({ "src": data });}function OnQueryFail() {alert("發生錯誤!");} </script>

三、Controller端代碼?

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;namespace WebApplication1.Controllers {public class HomeController : Controller{public ActionResult Index(){return View();}[HttpPost]public ActionResult UploadImg(long MouldId){string msg = string.Empty;if (Request.Files.Count > 0){HttpPostedFileBase file = Request.Files["file1"];if (file.ContentLength < 5 * 1024 * 1024){string fileType = System.IO.Path.GetExtension(file.FileName);//獲取文件類型if (!System.IO.Directory.Exists(Server.MapPath("~/Pictures/"))){System.IO.Directory.CreateDirectory(Server.MapPath("~/Pictures/"));}string filePath = Server.MapPath("~/Pictures/");//保存文件的路徑if (fileType != null){fileType = fileType.ToLower();//將文件類型轉化成小寫if ("(.gif)|(.jpg)|(.bmp)|(.jpeg)|(.png)".Contains(fileType)){file.SaveAs(filePath + file.FileName);string str = "Pictures/" + file.FileName;msg = str;}else{msg = "只支持圖片格式";}}}else{msg= "圖片大小不能超過5M";}}else{msg = "上傳圖片不能為空";}return Content(msg);}} }

參考:https://blog.csdn.net/weixin_44540201/article/details/89630530

總結

以上是生活随笔為你收集整理的ASP.NET MVC 上传图片到项目目录中的文件夹并显示的全部內容,希望文章能夠幫你解決所遇到的問題。

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