HTML upload file, background receive

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.