asp.net MVC實(shí)現(xiàn)無(wú)組件上傳圖片實(shí)例介紹
更新時(shí)間:2013年05月28日 16:55:18 作者:
無(wú)組件實(shí)現(xiàn)上傳圖片使用input的file作為上傳選擇文件,具體實(shí)現(xiàn)如下:前后臺(tái)代碼很詳細(xì),感興趣的朋友們可不要錯(cuò)過(guò)了哈
例子:
如我想上傳一個(gè)圖片到服務(wù)器端:asp頁(yè)面
<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>
后臺(tái)代碼
public ActionResult fileUpLoad(HttpPostedFileBase imageUpLoad(這里跟前臺(tái)頁(yè)面input輸入框name保持一致))
{
string fileName = imageUpLoad.FileName;
//轉(zhuǎn)換只取得文件名,去掉路徑。
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
//保存到相對(duì)路徑下。
imageUpLoad.SaveAs(Server.MapPath("../../image/img/" + fileName));
//以下代碼是將 路徑保存到數(shù)據(jù)庫(kù)。
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頁(yè)面
復(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>
后臺(tái)代碼
復(fù)制代碼 代碼如下:
public ActionResult fileUpLoad(HttpPostedFileBase imageUpLoad(這里跟前臺(tái)頁(yè)面input輸入框name保持一致))
{
string fileName = imageUpLoad.FileName;
//轉(zhuǎn)換只取得文件名,去掉路徑。
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
//保存到相對(duì)路徑下。
imageUpLoad.SaveAs(Server.MapPath("../../image/img/" + fileName));
//以下代碼是將 路徑保存到數(shù)據(jù)庫(kù)。
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的實(shí)體繼承模式
本文詳細(xì)講解了Entity?Framework使用Code?First的實(shí)體繼承模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
為大家講解下Repeater綁定dictionary數(shù)據(jù)源以及報(bào)錯(cuò)處理的方法,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04asp.net中JavaScript數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)代碼
我對(duì)JavaScript一直不了解。常常為了一點(diǎn)點(diǎn)的數(shù)據(jù)驗(yàn)證和無(wú)刷新就去動(dòng)用AJAX,實(shí)在不爽——有點(diǎn)殺雞用牛刀的感覺(jué)。2010-05-05asp.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)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12Asp.net中獲取應(yīng)用程序完整Url路徑的小例子
Asp.net中獲取應(yīng)用程序完整Url路徑的小例子,需要的朋友可以參考一下2013-06-06