當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用JavaScript调用aspx后台代码
生活随笔
收集整理的這篇文章主要介紹了
使用JavaScript调用aspx后台代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法1:js同步調用
觸發:
οnclick="javascript:share('<%# Eval("id_File") %>')"頁面函數:
function share(id_File) {var a = '<%= ShareDoc("' + id_File + '") %>';alert(a);}后臺cs函數:
public string ShareDoc(string id_File){return "后臺函數[" + id_File + "]";}?
方法2:JQuery ajax異步調用
觸發:
οnclick="javascript:download('<%# Eval("FileNewName") %>','<%# Eval("FileExtName") %>')"前臺函數:
function download(p1, p2) {$.ajax({url: 'MyDocList.aspx/DownloadDoc',contentType: 'application/json; charset=utf-8',datatype: 'json',type: 'POST',data: '{FileNewName:"' + p1 + '",FileExtName:"' + p2 + '"}', //參數success: function(result) {//成功后執行的方法alert(result.d); // 后臺返回值 },error: function(result) {//失敗執行的方法alert("error:" + result.responseText);}});}后臺cs函數:
[WebMethod]public static string DownloadDoc(string FileNewName, string FileExtName){string filePath = Server.MapPath(string.Format("~/FileUpload/{0}.{1}", FileNewName, FileExtName));if (System.IO.File.Exists(filePath)){FileInfo file = new FileInfo(filePath);Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解決中文亂碼Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解決中文文件名亂碼 Response.AddHeader("Content-length", file.Length.ToString());Response.ContentType = "appliction/octet-stream";Response.WriteFile(file.FullName);Response.End();}return filePath;}注意:下載功能是錯誤的。但是函數可以這樣調用。
?
轉載于:https://www.cnblogs.com/cocoulong/p/3167448.html
總結
以上是生活随笔為你收集整理的使用JavaScript调用aspx后台代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习使用Free RTOS ,移植最新的
- 下一篇: Linux 获取设备树源文件(DTS)里