How to download file from System.Web.HttpContext.Current.Response.BinaryWrite(byteArray); from javascript instead of C#

StackOverflow https://stackoverflow.com/questions/17794166

Domanda

I have a C# method that works if you call it from another C# method, but not from javascript. How do I make it work from my ajax call?

I need to get a file based on an ID that is passed in, so I have an ajax post with the statusID. That ID is pulling the proper file in my C# method, it is just not giving the file save dialog.

However, if I call this from my C# page load method with a static statusID for testing purposes, it works just fine.

Here is the C# method:

    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();
        }
    }

Here is how I am calling it from my page, in 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) {

        }
    });
}

Nothing happens. In Chrome, I don't see anything in the javascript console, and in IE, my console spits this out: "XML5619: Incorrect document syntax."

Again, if I go into the controller, and call the method in my page load method, it presents the save file dialog and saves the file just fine. So I must be doing something wrong with my javascript/jquery/ajax...

I am new to MVC4 and know I'm missing something here. What am I missing?

È stato utile?

Soluzione 2

Here's one suggestion, largely based on answer from LastCoder:

Decorate your action with the [HttpGet] attribute and change the parameter name to id:

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

Now in your client-side code, simply do this:

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

Altri suggerimenti

Use window.open('CONTROLLER_URL/STATUS_ID'); instead of an AJAX request.

<a target="_blank" href="javascript:window.open('/Home/Get_Attachment_By_StatusID/12345');">Test</a>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top