&nbs
p; function upload() {
VAR options = {
ty
PE: "POST", //当然这个是传送方式
url: '
../Include/Files.ashx', //一般处理程序的路径
success: function (msg) { //返回的参数
$("
#server_
img").attr("src", msg); //回显图片。
}
};
// 将options传给ajaxForm
$('#aspnetForm').ajaxS
ubm
IT(options);
}
public void
PRocessRequest(HttpContext context)
{
HttpFileCollection files = context.Request.Files; //
From中获取文件对象
if (files.Count > 0)
{
string path = ""; //路径字符串
Random rnd = new Random();
for (int i = 0; i < files.Count;
i++)
{
HttpPoste
DFile file = files[i]; //得到文件对象
if (file.ContentLength > 0)
{
string fileN
ame = file.FileName;
string extension = Path.GetExtension(fileName);
int num = rnd.Next(5000, 10000); //文件名称
path = "../../UserFiles/temp/" + num.ToString() + extension;
file.SaveAs(
System.Web.HttpContext.
current.Server.MapPath(path)); //
保存文件。
}
}
context.Response.Write(path); //返回文件存储后的路径,用于回显。
}
}
<img id="server_img" width="
360px" style="border: 1px solid #ccc; padding: 2px;" title="" alt="" /> //用于回显图片
<asp:FileUpload ID="Up_load" runat="server" onchange="upload()" ontextchange="upload()"/> //上传图片,自动的,两个事件是为了保证所有浏览器都兼容。