I created a webpage where a PDF file will be displayed after a QR code has been scanned. When the QR code is scanned it shows a URL and when the URL is clicked it redirects to the page where the PDF file will show.
It shows in laptop and desktop devices but when I used a mobile phone to scan the QR code and navigate to the URL, the PDF file did not show.
I used Handler.ashx I don't know if Handler.ashx can show on a mobile deivce.
Here is the page where the Pdf will show
HTML
<div class="container" id="centered" style="margin: 0 auto; padding: 5px; font-family: Nunito; border: 1px solid #d9dde4;">
<div class="form-horizontal">
<div class="contentt">
<asp:Literal ID="ltEmbed" runat="server" />
</div>
</div>
</div>
MY Handler.ashx
public class Receipt : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
int id = int.Parse(context.Request.QueryString["Id"]);
byte[] bytes;
string contentType;
string constr = "MyConnectionString";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM Table 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["Contyp"].ToString();
}
con.Close();
}
}
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Session["receipt"] != null)
{
BindCardAndQR();
}
}
public string ToAbsoluteUrl(string relativeUrl)
{
if (string.IsNullOrEmpty(relativeUrl))
{
return relativeUrl;
}
if (relativeUrl.StartsWith("/"))
{
relativeUrl = relativeUrl.Insert(0, "~");
}
if (!relativeUrl.StartsWith("~/"))
{
relativeUrl = relativeUrl.Insert(0, "~/");
}
var url = HttpContext.Current.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
return String.Format("{0}://{1}{2}{3}", url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
}
private void BindCardAndQR()
{
if (Session["receipt"] != null)
{
int id = Convert.ToInt32(Session["receipt"]);
string sql = "SELECT * FROM Table WHERE Id = @Id";
SqlParameter[] parameters = new[] { new SqlParameter("@Id", id) };
// Bind Image3 for Card Image
DataTable dt = SelectFromDatabase(sql, parameters);
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\"></object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("~/UIDocument.aspx?Id=" + id));
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}
}
public byte[] GetQRCodeBytes(string url)
{
//QR code generated here
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection("MyConnectionString"))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (parameters != null)
{
cmd.Parameters.AddRange(parameters);
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}