Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have one button on my partial view,I want to open pdf file on click of button
XML
<button type="button" class="CleanButton" name="button" onclick="ValidateFaxReports('/Verification/PreviewFax','faxReportsComments','FaxReports');">
                           Preview Fax
                       </button>

Onclick of the button I need to call javascript function, to check some validation,
'/Verification/PreviewFax' is Url, faxReportsComments is my form Id, FaxReports is id of div which I am using in main view.
Here is my function

F#
private string ViewReport(string file)
        {
            string errorMessage = string.Empty;
            byte[] buffer = null;
            try
            {
                if (file.ToLower().Contains(".enc"))
                {
                    buffer = VOEI.DAL.Crypto.DecryptBinaryFileToByteArray(file);
                    file = file.ToLower().Replace(".enc", String.Empty);
                }
                else
                {
                    FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                    BinaryReader binReader = new BinaryReader(fs);

                    buffer = binReader.ReadBytes((int)fs.Length);
                    binReader.Close();
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                errorMessage = e.ToString();
                return errorMessage;
            }

            try
            {
                Response.Clear();
                Response.Buffer = true;

                Response.ClearHeaders();
                Response.ClearContent();

                Response.AddHeader("content-disposition", "attachment; filename=" + System.IO.Path.GetFileName(file) + "");
                //Set the appropriate ContentType.
                Response.ContentType = "Application/pdf";

                Response.BinaryWrite(buffer);
                Response.End();
            }
            finally
            {
                //System.IO.File.Delete(file);
            }
            return errorMessage;
        }


But it's not working, I don't know why?
Am I missing something?

Rachana
Posted
Comments
[no name] 23-Jul-12 17:38pm    
"But it's not working"... does not tell anyone anything about what the problem is.

1 solution

I have no idea what is going on here, because you're lacking details. Is this an AJAX call ( it won't work ) ? Where is the javascript method ? What sort of page request is being made here and what does it return ? The ActionResult class has a derived class called FileResult, which is what I think you need ( and to call it, not an AJAX call )
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900