The example in this article describes the Js+ajax method for obtaining file size. Share to everyone for your reference, specific as follows:
As the name suggests, through JS and Ajax to get the size of uploaded files, before uploading can have a judge, the upload of the file has control, because JS control file size (JS get file size) Some problems, specific everyone try to know, here to tidy up the Ajax to obtain file size method, more useful , and then debug the process, there have been c:/fakepath/errors, but also the solution listed below, for your reference
Less nonsense, the code is as follows
JS is as follows:
<script language= "Jscript" >
function Chksize () {
var tickettype = "Oil";
var File1=document.getelementbyid ("txtfile");
var v = file1.value;
File1.select ();
var Realpath = Document.selection.createRange (). text;
var input = "<Record><Type>TicketLoanResult</Type><TicketType>" + Realpath + "</tickettype ></Record> ";
var objxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Objxmlhttp.open ("POST", "xmlhttppost.aspx", false);
Objxmlhttp.send (input);
var returnxml = Objxmlhttp.responsetext;
if (returnxml!=null)
{
returnxml=parseint (returnxml/1024/1024);
}
alert (returnxml);
return false;
}
</script>
Background C # code:
Public partial class XMLHttpPost:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {try
{XmlDocument xmldocumenet = new XmlDocument ();
Xmldocumenet.load (Request.inputstream); String type = Xmldocumenet.selectsinglenode ("Descendant::type").
InnerText; String returnmessage = String.
Empty; Switch (type.
Trim ()) {case "Ticketloanresult": ReturnMessage = Getticketloanresult (xmldocumenet);
Break
Default:break;
} Response.ContentType = "Text/xml";
Response.Write (ReturnMessage); catch (Exception exceptional) {///If there is a bug return error information (XML format) string errormessage = "<Error>" + exceptional.
Message + "</Error>";
Response.ContentType = "Text/xml";
Response.Write (errormessage);
finally {Response.End (); } private String Getticketloanresult (XmlDocument input) {XmlNode item = input.
selectSingleNode ("Descendant::tickettype"); String TicketType = Item.
Innertext.trim (); FileSTREAM stream = new FileStream (Tickettype,filemode.open); return stream.
Length.tostring ();
}
}
Full instance code click here to download the site.
I hope this article will help you with JavaScript programming.