2012-02-08 57 views
0

我的web应用程序:我点击一个按钮,这个按钮调用一个web服务函数,这个函数创建一个文件(使用DocX dll)。我想下载这个刚刚通过网页浏览器创建的文件,在网上下载一个类似的文件。 如何做到这一点?如何下载在asp.net中创建该文件的文件

我下面

代码//点击按钮事件

$(frm_id+' #btn_eprt_tml') 
.button() 
.click(function(){ 
      //eprt_tml 
      $.ajax({ 
       type: "POST", 
       url: "JqueryFunction.aspx/eprt_tml", 
       data: "{ptcn_id:'"+ptcn_id+"'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function() { 
        alert("Completed"); 
       }, 
       error: function() { 
        alert("Not completed"); 
       } 
      }); 

// eprt_tml功能

[System.Web.Services.WebMethod] 
public static void eprt_tml(int ptcn_id) 
{ 
    DocX g_document; 
    try 
    { 
     // Store a global reference to the loaded document. 
     g_document = DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx"); 
     g_document = crt_from_tpl(DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx")); 
     // Save all changes made to this template as Invoice_The_Happy_Builder.docx (We don't want to replace InvoiceTemplate.docx). 
     //g_document.Save(); 
     g_document.SaveAs(@"D:\Project\CRM1\tml\Invoice_The_Happy_Builder.docx"); 
    } 
     // The template 'Template.docx' does not exist, so create it. 
    catch (FileNotFoundException) 
    { 

    } 
} 

// crt_from_tpl功能

[System.Web.Services.WebMethod] 
    private static DocX crt_from_tpl(DocX template) 
    { 
     template.AddCustomProperty(new CustomProperty("static_title", "afdaslfjlk")); 
     template.AddCustomProperty(new CustomProperty("tmlname", "asdfasdfasf")); 
     template.AddCustomProperty(new CustomProperty("tmlcontent", "asdfasd")); 
     template.AddCustomProperty(new CustomProperty("ptcnname", "asdasdfsd")); 
     template.AddCustomProperty(new CustomProperty("ptcntitle", "asdfasdfsad")); 
     template.AddCustomProperty(new CustomProperty("coursename", "asdfsadsdf")); 
     return template; 
    } 

回答

0

上创建的文件名该SessionID的

的基础上更换UR “tml_tpt” 到 “HttpContext.Current.Session.SessionID”

[System.Web.Services.WebMethod(EnableSession=true)] 
public static void eprt_tml(int ptcn_id) 
{ 
    DocX g_document; 
    try 
    { 
     // Store a global reference to the loaded document. 
     g_document = DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx"); 
     g_document = crt_from_tpl(DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx")); 
     // Save all changes made to this template as Invoice_The_Happy_Builder.docx (We don't want to replace InvoiceTemplate.docx). 
     //g_document.Save(); 
     g_document.SaveAs(@"D:\Project\CRM1\tml\Invoice_The_Happy_Builder.docx"); 
    } 
     // The template 'Template.docx' does not exist, so create it. 
    catch (FileNotFoundException) 
    { 

    } 
} 
+0

你能解释更多。我很开心。谢谢 – Hainlp 2012-02-08 08:17:01