asp.net MVC實(shí)現(xiàn)無組件上傳圖片實(shí)例介紹
更新時(shí)間:2013年05月28日 16:55:18 作者:
無組件實(shí)現(xiàn)上傳圖片使用input的file作為上傳選擇文件,具體實(shí)現(xiàn)如下:前后臺代碼很詳細(xì),感興趣的朋友們可不要錯(cuò)過了哈
例子:
如我想上傳一個(gè)圖片到服務(wù)器端:asp頁面
<form id="form1" runat="server" action="/bookIndex/fileUpLoad/(你準(zhǔn)備處理的 ActionResult)" method="post" enctype="multipart/form-data">
<input type="file" id="imageUpLoad" name="imageUpLoad">
<input type="button" value="點(diǎn)擊上傳" onclick="UpLoad()">
....
</form>
js代碼:
<script type="text/javascript">
function UpLoad()
{
如果有其他的值,判斷下是否為空.
form1.submit();
}
<script>
后臺代碼
public ActionResult fileUpLoad(HttpPostedFileBase imageUpLoad(這里跟前臺頁面input輸入框name保持一致))
{
string fileName = imageUpLoad.FileName;
//轉(zhuǎn)換只取得文件名,去掉路徑。
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
//保存到相對路徑下。
imageUpLoad.SaveAs(Server.MapPath("../../image/img/" + fileName));
//以下代碼是將 路徑保存到數(shù)據(jù)庫。
string ImagePath = "../../image/img/" + fileName;
string sql = "insert into bookinfo(bookphoto)values('" + ImagePath + "')";
//封裝好的代碼,直接調(diào)用。
DataBase db = new DataBase();
db.getConn();
int result = db.executeUpdate(sql);
return View();
}
如我想上傳一個(gè)圖片到服務(wù)器端:asp頁面
復(fù)制代碼 代碼如下:
<form id="form1" runat="server" action="/bookIndex/fileUpLoad/(你準(zhǔn)備處理的 ActionResult)" method="post" enctype="multipart/form-data">
<input type="file" id="imageUpLoad" name="imageUpLoad">
<input type="button" value="點(diǎn)擊上傳" onclick="UpLoad()">
....
</form>
js代碼:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function UpLoad()
{
如果有其他的值,判斷下是否為空.
form1.submit();
}
<script>
后臺代碼
復(fù)制代碼 代碼如下:
public ActionResult fileUpLoad(HttpPostedFileBase imageUpLoad(這里跟前臺頁面input輸入框name保持一致))
{
string fileName = imageUpLoad.FileName;
//轉(zhuǎn)換只取得文件名,去掉路徑。
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
//保存到相對路徑下。
imageUpLoad.SaveAs(Server.MapPath("../../image/img/" + fileName));
//以下代碼是將 路徑保存到數(shù)據(jù)庫。
string ImagePath = "../../image/img/" + fileName;
string sql = "insert into bookinfo(bookphoto)values('" + ImagePath + "')";
//封裝好的代碼,直接調(diào)用。
DataBase db = new DataBase();
db.getConn();
int result = db.executeUpdate(sql);
return View();
}
相關(guān)文章
設(shè)置默認(rèn)Ajax操作cache and error
設(shè)置默認(rèn)Ajax操作cache and error,需要的朋友可以參考一下2013-02-02
如何使用簽名保證ASP.NET MVC OR WEBAPI的接口安全
這篇文章主要介紹了如何使用簽名保證ASP.NET MVC OR WEBAPI的接口安全,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04
Entity?Framework使用Code?First的實(shí)體繼承模式
本文詳細(xì)講解了Entity?Framework使用Code?First的實(shí)體繼承模式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
為大家講解下Repeater綁定dictionary數(shù)據(jù)源以及報(bào)錯(cuò)處理的方法,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04
asp.net中JavaScript數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)代碼
我對JavaScript一直不了解。常常為了一點(diǎn)點(diǎn)的數(shù)據(jù)驗(yàn)證和無刷新就去動用AJAX,實(shí)在不爽——有點(diǎn)殺雞用牛刀的感覺。2010-05-05
asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法,涉及asp.net調(diào)用新浪接口獲取天氣預(yù)報(bào)信息的實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-12-12
Asp.net中獲取應(yīng)用程序完整Url路徑的小例子
Asp.net中獲取應(yīng)用程序完整Url路徑的小例子,需要的朋友可以參考一下2013-06-06

