Mike
Mike

Reputation: 751

C# Download and View pdf file

I want to download and view pdf file in browser with single click .. both download and view code is working on separate pages but it doesnt work simultaneously on button click cause of HttpContext.Current.Response any suggestion how can I handle it

below is code

public static void DownloadFile(string filePath)
{
    try {
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + System.IO.Path.GetFileName(filePath) + "\"");
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.WriteFile(filePath);
        HttpContext.Current.Response.End();

    } catch (Exception ex) {
    }

}


public static void ViewFile(string filePath)
{
    WebClient User = new WebClient();
    Byte[] FileBuffer = User.DownloadData(filePath);

    if (FileBuffer != null) {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-length", FileBuffer.Length.ToString());
        HttpContext.Current.Response.BinaryWrite(FileBuffer);
        HttpContext.Current.Response.End();
    }
    DownloadFile(filePath);
}

Upvotes: 0

Views: 4236

Answers (1)

Amit
Amit

Reputation: 890

Something like this -

If you want to download and open server file call SendFiletoBrowser. If you want remote file to be downloaded and displayed in browser then call OpenRemoteFileInBrowser method.

public void SendFiletoBrowser(string path,string fileName)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            using (FileStream fs = File.OpenRead(Server.MapPath(path)))
            {
                fs.CopyTo(ms);
            }
            ms.Position = 0;
            OpenInBrowser(ms, fileName);
        }
        catch (Exception)
        {

            throw;
        }


    }

    public void OpenRemoteFileInBrowser(Uri destinationUrl, string fileName)
    {
        try
        {
            WebClient wc = new WebClient();
            using (MemoryStream stream = new MemoryStream(wc.DownloadData(destinationUrl.ToString())))
            {
                OpenInBrowser(stream, fileName);
            }
        }
        catch (Exception)
        {

            throw;
        }

    }

    private void OpenInBrowser(MemoryStream stream, string fileName)
    {

        byte[] buffer = new byte[4 * 1024];
        int bytesRead;
        bytesRead = stream.Read(buffer, 0, buffer.Length);

        Response.Buffer = false;
        Response.BufferOutput = false;
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
        if (stream.Length != -1)
            Response.AppendHeader("Content-Length", stream.Length.ToString());

        while (bytesRead > 0 && Response.IsClientConnected)
        {
            Response.OutputStream.Write(buffer, 0, bytesRead);
            bytesRead = stream.Read(buffer, 0, buffer.Length);
        }

    }

Upvotes: 1

Related Questions