HTML code: I am sending a request to teacher_center.aspx, not to the. ASHX general handler, need to add runat= "Server", I will try to send the request to. ashx
<input type= "file" class= "Upload" id= "fu_book_img" name= "fu_book_img" runat= "Server" onchange= "selectbookimg ()"/ > Upload Images
<iframe id= ' framefile ' name= ' framefile ' style= ' display:none; ' ></iframe>
<input type= "hidden" id= "hidden_select_img"/>
JS script:
function Selectbookimg (e) {
var fname = ' <%=datetime.now.tostring ("Yyyymmddhhmmss")%> ' + getrandom (999);
var oldselect = $ ("#hidden_select_img"). Val ();//The Last selected picture
$ ("#form1"). attr ("Action", "teacher_center.aspx?op=upload&fname=" + fname + "&oldselect=" + oldselect);
$ ("#form1"). attr ("Target", "framefile");//hidden IFrame, in order to achieve no refresh, refer to my another article
$ ("#form1"). Submit ();
$ ("#form1"). Removeattr ("target");
}
function Setimg (imgurl) {
$ ("#books_img"). attr ("src", imgurl);
$ ("#hidden_select_img"). Val (Imgurl);
}
. NET code
Httppostedfile fu = request.files["fu_book_img"];//name property
if (!string. IsNullOrEmpty (FU. FileName))
{
String ext = path.getextension (fu. FileName). ToLower ();
if (CommonHelper.ImgPattern.Contains (EXT))//Determine the picture format is correct
{
Verify the file size, Fu. ContentLength is the file byte length
if (FU. ContentLength > (COMMONHELPER.HEADIMGSIZE/2))
{
Jscript.show (This, "You cannot upload a file larger than 1M, please re-upload!") ");
}
Else
{
String path = Server.MapPath ("~/uploadfile/bookimg/temp/") + fname + ext;
Fu. SaveAs (path);
Delete the last selected file
String oldpath = Server.MapPath ("~" + oldselect);
if (file.exists (OldPath))
{
File.delete (OldPath);
}
Loading the selected picture into the IMG in the page requires window.parent, because we specify that the IFRAME generation is submitted
Response.Write ("<script>window.parent.setimg ('/uploadfile/bookimg/temp/" + fname + ext + "');</script>") ;
Response.End ();
}
}
Else
{
Jscript.show (this, "image format is incorrect");
}
}
Else
{
Jscript.show (This, "Please select Picture");
}
HTML upload file, background receive