0

In my webForm project as follow :

  1. I have a button which user click on it
  2. call a web method (via jquery request) to extract some binary data from sql server as pdf
  3. Then give a user to download/view the pdf via browser.

This works for my development machine. However, this fails on production server and give to user an empty pdf!

Where is my problem & how to solve it?

Here is jquery request to my web method :

function OpenAttachment(jsonData){
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "MedicalHistory.aspx/OpenAttachment",
                data: JSON.stringify({ objData: jsonData }),
                dataType: "json",
                success: function (response) {
                    gdv.PerformCallback('downloadfile|' + response.d);
                },
                error: function (request, status, error) {
                    alert('error : ' + error);
                }
            });
        }

Here is my web method :

[WebMethod]
        public static string OpenAttachment(object objData)
        {
            string strResult = string.Empty;

            if (objData == null)
                return strResult;

            DataTable dtResult = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(objData.ToString());
            string strDocContent = dtResult.Rows[0]["DocContent"].ToString();
            string strFileName = dtResult.Rows[0]["FileName"].ToString();

            byte[] pdfByteArray = Encoding.Default.GetBytes(strDocContent);
            HttpContext.Current.Session["_binDownloadData"] = pdfByteArray;

            strResult = strFileName;
            return strResult;
        }

And here is download code in my download page :

object objDownloadData = Session["_binDownloadData"];
                if (objDownloadData != null)
                {
                    byte[] pdfByteArray = (byte[])objDownloadData;
                    string strContentType = string.Empty;

                    if (strFileType == "pdf")
                    {
                        strContentType = "application/pdf";
                    }

                    #region Downloading file

                    HttpContext.Current.Response.Clear();
                    MemoryStream ms = new MemoryStream(pdfByteArray);
                    HttpContext.Current.Response.ContentType = strContentType;
                    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", strFileName));
                    HttpContext.Current.Response.Buffer = true;
                    ms.WriteTo(HttpContext.Current.Response.OutputStream);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.SuppressContent = true;
                    HttpContext.Current.ApplicationInstance.CompleteRequest();

                    #endregion

                    Session["_binDownloadData"] = null;
                }

Thanks in advance.

hdv212
  • 61
  • 10

1 Answers1

0

Try with changing contentType and dataType. $.ajax - dataType

function OpenAttachment(jsonData){
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "MedicalHistory.aspx/OpenAttachment",
                data: JSON.stringify({ objData: jsonData }),
                dataType: "application/pdf",
                success: function (response) {
                    gdv.PerformCallback('downloadfile|' + response.d);
                },
                error: function (request, status, error) {
                    alert('error : ' + error);
                }
            });
        }
Thomson Mixab
  • 657
  • 4
  • 8
  • Thanks for reply, But when changed my code as your advised, My .net web method (OpenAttachment) not being called! Any idea? – hdv212 Aug 06 '21 at 16:04