SoFunction
Updated on 2025-02-28

Javascript using ajax and C# to get file size instances

Javascript ajax implements a method to get file size.

As the name suggests, you can get the size of the uploaded file through JS and Ajax. Before uploading, you can make a judgment to control the uploaded file. Because there are some problems with js controlling the file size (JS obtaining the file size), you will know the specific one. Here I sorted out the method of obtaining the file size of ajax, which is easier to use. During the debugging process, a c:/fakepath/ error occurred. The solution is also 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=("txtfile");
 var v = ;
 ();
 var realpath = ().text;
 var input = "<Record><Type>TicketLoanResult</Type><TicketType>" + realpath + "</TicketType></Record>";
 var objXmlHttp = new ActiveXObject("");
 ("POST", "", false);
 (input);
 var returnXml = ;
 if(returnXml!=null)
 {
  returnXml=parseInt(returnXml/1024/1024);
 }
 alert(returnXml); 
 return false; 
}
</script>
 

Background C# code:

public partial class XMLHttpPost : 
{
 protected void Page_Load(object sender, EventArgs e)
 {
 try
 {
  XmlDocument xmlDocumenet = new XmlDocument();
  ();
  string type = ("descendant::Type").InnerText;
  string returnMessage = ;
  switch (())
  {
  case "TicketLoanResult":
   returnMessage = GetTicketLoanResult(xmlDocumenet);
   break;
  default:
   break;
  }
   = "text/xml";
  (returnMessage);
 }
 catch (Exception exceptional)
 {
  //If there is an error, return error information (Xml format)  string errorMessage = "&lt;Error&gt;" +  + "&lt;/Error&gt;";
   = "text/xml";
  (errorMessage);
 }
 finally
 {
  ();
 }
 }
 private string GetTicketLoanResult(XmlDocument input)
 {
 XmlNode item = ("descendant::TicketType");
 string ticketType = ();
 FileStream stream = new FileStream(ticketType,);
 return ();
 }
}

js ajax gets file size, instance code!

Thank you for reading, I hope it can help you. Thank you for your support for this site!