如何在Javascript 中利用ajax和C#获取文件的大小?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
Java是什么
Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。
JS如下:
<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>
后台C#代码:
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();
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注天达云行业资讯频道,感谢您对天达云的支持。