Ajax+ashx Perfect implementation of input file upload files

Source: Internet
Author: User
Tags save file

1. input file style does not meet the requirements

<type= "file"  value= "Browse"/>

IE8: F Irefox: Chrome:   

2. Input File Upload button beautification

Css:

. File{position:relative;Background-color:#b32b1b;Border:1px solid #ddd;width:68px;Height:25px;Display:Inline-block;text-decoration:None;text-indent:0;Line-height:25px;font-size:14px;Color:#fff;margin:0 Auto;cursor:Pointer;text-align:Center;Border:None;Border-radius:3px; }. File Input{position:Absolute;Top:0; Left:-2px;Opacity:0;width:10px;}

Html:

<Div>    <span>Select File:</span><inputID= "Txt_filepath"type= "text"ReadOnly= "ReadOnly"/>    <aclass= "File"><inputID= "Btnfile"name= "Btnfile"type= "File"/>Browse</a>
</Div>

After the beautification:

3, ajax+ashx implement upload function

Introduction File: Jquery-1.11.3.js ajaxfileupload.js

Js:

$(function () {            //Select File$ (". File"). On ("Change", "input[type= ' file ']",function () {                varFilePath = $ ( This). Val (); //set upload file type                if(Filepath.indexof ("xls")! =-1 | | filepath.indexof ("xlsx")! =-1) {                    //Uploading Files$.ajaxfileupload ({URL:' Ashx/filehandler.ashx ', Secureuri:false, Fileelementid:' Btnfile ', DataType:' JSON ', Success:function(data, status) {//get the upload file path$ ("#txt_filePath"). Val (Data.filenewname); Alert ("File upload is successful!" "); }, Error:function(data, status, E) {alert (e);                }                    }); } Else{alert ("Please choose the correct file format!" "); //empty the upload path$ ("#txt_filePath"). Val (""); return false;        }            }); })
Filehandler.ashx
 Public classFilehandler:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; stringmsg =string.        Empty; stringError =string.        Empty; stringresult =string.        Empty; stringFilePath =string.        Empty; stringFilenewname =string.        Empty; //This can only be done with <input type= "file"/>, because the server control is a httpinputfile typehttpfilecollection files =context.        Request.Files; if(Files. Count >0)        {            //Set file nameFilenewname = DateTime.Now.ToString ("YYYYMMDDHHMMSSFF") +"_"+ System.IO.Path.GetFileName (files[0].            FileName); //Save Filefiles[0]. SaveAs (context. Server.MapPath ("~/upload/"+filenewname)); Msg="File Upload Successful! "; Result="{msg: '"+ msg +"', Filenewname: '"+ Filenewname +"'}"; }        Else{Error="File upload failed! "; Result="{error: '"+ Error +"'}"; } context.        Response.Write (Result); Context.    Response.End (); }      Public BOOLisreusable {Get {            return false; }    }}

Implement a simple upload function

Ajax+ashx Perfect implementation of input file upload files

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.