Click here to Skip to main content
13,799,679 members
Rate this:
 
Please Sign up or sign in to vote.
I've tried to download a specific file from a SQL Server database using Ajax and a web service and a code is working without errors and still can't download the file.Here is the code


What I have tried:

My HTML
<pre><input id="btn_download" type="button" value="download_att" />

my ajax function to read id for file retrive

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<pre lang="Javascript"><pre lang="Javascript">$('#btn_download').click(function () {
                id = $('#Tid').val();
                $.ajax({
                    url: 'WebService1.asmx/DownloadFile',
                    method: 'POST',
                    contentType: 'application/json;charset=utf-8',
                    data: '{id:' + JSON.stringify(id) + '}',
                    success: function () {
                        alert("s");
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            });

my webservice

[ScriptMethod]
[WebMethod]
public void DownloadFile(int id )
{
    byte[] bytes;

    string fileName, contentType;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TesterConnectionString1"].ConnectionString);

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "select image from attach where Id=@Id";
        cmd.Parameters.AddWithValue("@Id", id);
        cmd.Connection = con;

        con.Open();

        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            sdr.Read();
            bytes = (byte[])sdr["image"];
            contentType = sdr["id"].ToString();
            fileName = sdr["id"].ToString();
        }

        con.Close();
    }


    httpContext.Current.Response.Clear();
    httpContext.Current.Response.Buffer = true;
    httpContext.Current.Response.Charset = "";
    httpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    httpContext.Current.Response.ContentType = contentType;
    httpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    httpContext.Current.Response.BinaryWrite(bytes);
    httpContext.Current.Response.Flush();
    httpContext.Current.Response.End();
}
Posted 2-Jan-18 19:53pm
Comments
Karthik Bangalore 3-Jan-18 1:46am
   
I dont think this method will work. create a page and load it inside iframe to download which will be very simple and easier.
HelloIt'sMe_M 3-Jan-18 2:03am
   
why not work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.181207.3 | Last Updated 3 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100