This article introduces an example of js ajax obtaining file size, involving JavaScript calling ajax interaction and background C# file operation related skills. Friends in need can refer to the following
Javascript ajax method to obtain file size .
As the name suggests, you can get the size of the uploaded file through JS and Ajax. You can make a judgment before uploading and control the uploaded file, because there are some problems with js controlling the file size (JS getting the file size). Specifically You will know after trying it. Here I have compiled the ajax method of obtaining the file size. It is relatively easy to use. During the debugging process, an error of c:/fakepath/ occurred. The solution is also listed below for everyone's benefit. Reference
Stop talking 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>
Backend 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) { //如果有錯誤則返回錯誤信息(Xml格式) 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(); } }
js ajax to get the file size, example code!
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
AJAX uses post to send data in xml format to receive data
ajax implements input box text change and display drop-down List effect
The above is the detailed content of Javascript implementation of obtaining file size using ajax and C#. For more information, please follow other related articles on the PHP Chinese website!