2013-07-22 54 views
0

我有一个C#方法,如果你从另一个C#方法调用它,但不是从JavaScript调用。我如何使我的ajax调用工作?如何从System.Web.HttpContext.Current.Response.BinaryWrite(byteArray)中下载文件;从JavaScript而不是C#

我需要基于传入的ID来获取文件,所以我有一个带有statusID的ajax文章。该ID正在我的C#方法中拉取正确的文件,它只是不提供文件保存对话框。但是,如果我从我的C#页面加载方法调用此方法并使用静态statusID进行测试,它就可以正常工作。

这里是C#方法:

public void Get_Attachment_By_StatusID(int statusID) 
    { 
     SqlDataReader _reader = null; 
     string _connString = "Data Source=133.31.32.33;Initial Catalog=Reports;Integrated Security=True"; 

     string sql = "SELECT a.StatusID ,a.DocumentName ,a.MIMETypeID ,a.Binary ,a.CreatedDate ,a.CreatedBy " + 
      ",b.MIMEType FROM Attachments a Inner join MIME_Types b on a.MIMETypeID = b.ID " + 
      "WHERE [StatusID] = {0} "; 

     sql = string.Format(sql, statusID); 

     try 
     { 
      _connection = new SqlConnection(_connString); 
      _connection.Open(); 

      using (SqlCommand cmd = new SqlCommand(sql, _connection)) 
      { 
       cmd.CommandType = System.Data.CommandType.Text; 
       _reader = cmd.ExecuteReader(); 

       if (_reader.HasRows) 
       { 
        while (_reader.Read()) 
        { 
         System.Web.HttpContext.Current.Response.ClearContent(); 
         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 
         System.Web.HttpContext.Current.Response.ContentType = _reader["MIMEType"].ToString(); 
         System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + _reader["DocumentName"].ToString() + ";"); 
         byte[] b = (byte[])_reader["Binary"]; 
         System.Web.HttpContext.Current.Response.BinaryWrite(b); 
         System.Web.HttpContext.Current.Response.Flush(); 
         System.Web.HttpContext.Current.Response.Close(); 
        } 

       } 

       _reader.Close(); 
       _connection.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      //Log exception to error Logging app 
      string err = ex.ToString(); 

     } 
     finally 
     { 
      if (_connection != null) 
       _connection.Close(); 
      if (_reader != null) 
       _reader.Close(); 
     } 
    } 

这里是如何我从我的页面调用它,在javascript:

function GetFile(statusID) { 
    var url = '/Home/Get_Attachment_By_StatusID'; 

    $.ajax({ 
     url: url, 
     type: 'post', 
     cache: false, 
     data: JSON.stringify({ "statusID": statusID }), 
     contentType: 'application/json', 
     success: function (data) { 

     } 
    }); 
} 

什么也没有发生。在Chrome浏览器中,我没有在JavaScript控制台中看到任何内容,而在IE中,我的控制台吐露了这些:“XML5619:错误的文档语法。”

再一次,如果我进入控制器,并调用我的页面加载方法的方法,它会显示保存文件对话框,并保存文件就好了。所以我一定是做错了我的javascript/jquery/ajax ...

我是新来的MVC4,知道我在这里失去了一些东西。我错过了什么?

+1

为什么要发送J SON到URL?它是否与“正常”POST查询字符串?尝试使用'data:{statusID:statusID}'(并丢失'contentType')。 –

+0

我会改变你的行动来接受GET请求,而不是'POST',并简单地改变你的客户端上的代码来做'window.location = path/to/action_to_download_file /;' – Icarus

+0

@Rocket Hazmat,statusID得到那里很好,字节数组填充了正确的文件。 – codesforcoffee

回答

1

这里有一个建议,主要是基于回答LastCoder:

装饰与[HttpGet]属性你的行动和改变参数名称id

[HttpGet] 
public void Get_Attachment_By_StatusID(int id) ... 

现在,在您的客户端代码,只需执行以下操作:

function GetFile(statusID) { 
    var url = '/Home/Get_Attachment_By_StatusID/'+statusID; 
    window.location=url; 
} 
+0

- 感谢您试图回答我的问题给LastCoder,但我做了你所说的并且仍然在我的Get_Attachment_By_StatusID方法中得到一个id参数为null。 – codesforcoffee

+0

你用'[HttpGet]'属性装饰过它吗?并将参数名称更改为'id'?如果是这样,请发布您的更新代码。 – Icarus

+0

我做了,但它没有工作,直到我进入我的路由选项,并添加ID作为我的MVC项目的可选参数。 在RouteConfig - > RegisterRoutes,我现在有这个,它的工作原理! routes.MapRoute( name:“Default”, url:“{controller}/{action}/{id}”, 默认值:new {controller =“Home”,action =“Home”,id = UrlParameter。可选的 } – codesforcoffee

2

使用window.open('CONTROLLER_URL/STATUS_ID');而不是AJAX请求。

<a target="_blank" href="javascript:window.open('/Home/Get_Attachment_By_StatusID/12345');">Test</a> 
+0

谢谢,我会去试试这个! – codesforcoffee

+0

+1,但我会删除'href =“javascript:...”'并将它放在外面以保持Javascript代码与标记分开。 – Icarus

+0

@LastCoder你可以给我一个在我的c#方法中改变什么的样本,使这个工作?现在它抱怨statusID为空。如何使用/ Home/Get_Attachment_By_StatusID /?后的URL添加到URL中? – codesforcoffee

相关问题