![](/img/trans.png)
[英]How to store image to folder and store the path to database using Ajax?
[英]how to store path in database and image in folder in asp.net with the help of javascript
我想在asp.net中將文件(即圖像)存儲在文件夾中,並在數據庫中存儲路徑,我想使用java腳本。 您能幫我如何在Java腳本中獲取圖像完整路徑的值。 我知道由於安全原因我們無法獲得完整的路徑,是真的可以避免這種情況。
為此目的,它很容易使用jQuery Forms插件 。 我們可以在asp網絡表單中使用它,如下所示。
HTML / ASPX頁面
<form id="myForm" action="fileupload.ashx" method="post">
Name: <input type="text" name="name" />
File: <input type="file" name="filetoupload" />
<input type="submit" value="Submit Comment" />
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function () {
// attach handler to form's submit event
$('#myForm').submit(function () {
// submit the form
$(this).ajaxSubmit();
// return false to prevent normal browser submit and page navigation
return false;
});
});
</script>
本示例中使用的純HTML。 如果是aspx webforms,則頁面中將只有一個form標簽。
將WebHandler(擴展名為.ashx)添加到網站。
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
HttpPostedFile MyFile=context.Request.Files["filetoupload"];
if (MyFile.ContentLength > 0 && MyFile != null)
{
MyFile.SaveAs(context.Server.MapPath("Path/On/Server"));
}
context.Response.Write("Saved Successfully");
}
public bool IsReusable {
get {
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.