asp.net MVC實現(xiàn)無組件上傳圖片實例介紹
更新時間:2013年05月28日 16:55:18 作者:
無組件實現(xiàn)上傳圖片使用input的file作為上傳選擇文件,具體實現(xiàn)如下:前后臺代碼很詳細,感興趣的朋友們可不要錯過了哈
例子:
如我想上傳一個圖片到服務(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="點擊上傳" 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();
}
如我想上傳一個圖片到服務(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="點擊上傳" 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-04Entity?Framework使用Code?First的實體繼承模式
本文詳細講解了Entity?Framework使用Code?First的實體繼承模式,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03Repeater綁定dictionary數(shù)據(jù)源代碼及報錯解決
為大家講解下Repeater綁定dictionary數(shù)據(jù)源以及報錯處理的方法,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04asp.net中JavaScript數(shù)據(jù)驗證實現(xiàn)代碼
我對JavaScript一直不了解。常常為了一點點的數(shù)據(jù)驗證和無刷新就去動用AJAX,實在不爽——有點殺雞用牛刀的感覺。2010-05-05asp.net實現(xiàn)根據(jù)城市獲取天氣預(yù)報的方法
這篇文章主要介紹了asp.net實現(xiàn)根據(jù)城市獲取天氣預(yù)報的方法,涉及asp.net調(diào)用新浪接口獲取天氣預(yù)報信息的實現(xiàn)技巧,非常簡單實用,需要的朋友可以參考下2015-12-12Asp.net中獲取應(yīng)用程序完整Url路徑的小例子
Asp.net中獲取應(yīng)用程序完整Url路徑的小例子,需要的朋友可以參考一下2013-06-06