C
C#14mo ago
tropic

❔ Image is not being displayed On Azure

In an application we get images of pdf files from the database in the form of a byte[]. When you use the preview button it is to open a new tab and display that file. IN development it shows up fine but once it is deployed to Azure on app service, Its just a blank page with no URL or any thing. I am requesting help debugging it.
protected void cppreview_Callback(object sender, CallbackEventArgsBase e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{
byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{

Session["PdfFile"] = fileBytes;
Session["PdfId"] = id;
_name = "View/Attachment/PdfFile.aspx";
}
}

cppreview.JSProperties["cpResult"] = _name;
}
}
protected void cppreview_Callback(object sender, CallbackEventArgsBase e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{
byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{

Session["PdfFile"] = fileBytes;
Session["PdfId"] = id;
_name = "View/Attachment/PdfFile.aspx";
}
}

cppreview.JSProperties["cpResult"] = _name;
}
}
31 Replies
tropic
tropic14mo ago
Things that I have tried I tried to use this method but that just returns a loading screen
context.Response.Clear();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(_cachedBinaryResources[imageName]);
context.Response.Flush();
context.Response.Clear();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(_cachedBinaryResources[imageName]);
context.Response.Flush();
I also tried to use this method as well
protected void btnDisplayPdf_Click(object sender, EventArgs e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{
byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{
// Get the byte array representing the PDF content
byte[] pdfBytes = fileBytes;

// Convert the byte array to a Base64 string
string base64String = Convert.ToBase64String(pdfBytes);

// Create a data URL with the image/jpeg content type
string dataUrl = $"data:image/png;base64,{base64String}";

// Register a startup script to open the data URL in a new window or tab
string script = $"window.open('{dataUrl}');";
ScriptManager.RegisterStartupScript(this, GetType(), "OpenImageScript", script, true);
}
}

}
}
protected void btnDisplayPdf_Click(object sender, EventArgs e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{
byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{
// Get the byte array representing the PDF content
byte[] pdfBytes = fileBytes;

// Convert the byte array to a Base64 string
string base64String = Convert.ToBase64String(pdfBytes);

// Create a data URL with the image/jpeg content type
string dataUrl = $"data:image/png;base64,{base64String}";

// Register a startup script to open the data URL in a new window or tab
string script = $"window.open('{dataUrl}');";
ScriptManager.RegisterStartupScript(this, GetType(), "OpenImageScript", script, true);
}
}

}
}
For added context I did not develop this app and it was inherited from another programmer who ended with the company on bad terms so no support from the original.
tropic
tropic14mo ago
What is generated
tropic
tropic14mo ago
What is supposed to be there
Mayor McCheese
Mayor McCheese14mo ago
I've not done webforms for some time, but imho you're abusing the aspx page and competing with page lifecycle issues, I'd try this as an ashx page for displaying the binary content.
tropic
tropic14mo ago
Fair, I just inherited this application from a dev that left on bad terms So no support there to explain what anything in this application is
Mayor McCheese
Mayor McCheese14mo ago
Is there anything in commit history?
tropic
tropic14mo ago
So, he did have a git repo, but when we attained the code he left the zip file without the .git folder
Mayor McCheese
Mayor McCheese14mo ago
Also what are you using in azure
tropic
tropic14mo ago
App Service So we are working with an app no documentation sadly and no commit history
Mayor McCheese
Mayor McCheese14mo ago
Seriously? Wtf, I'm so sorry to hear that.
tropic
tropic14mo ago
Yeah it sucks
Mayor McCheese
Mayor McCheese14mo ago
So app services are backed by an app service plan, do you know the app service plan sku/os? I'm not sure if you can easily change oS if it's not windows
tropic
tropic14mo ago
Os is windows
Mayor McCheese
Mayor McCheese14mo ago
Ugh
tropic
tropic14mo ago
SKU would be the default Basic (B1) Yeah, I know xD
Mayor McCheese
Mayor McCheese14mo ago
Sku won't help, but I figured on the off chance something doesn't work as expected on Linux with web forms.
tropic
tropic14mo ago
Yeah
Mayor McCheese
Mayor McCheese14mo ago
Any log messages?
tropic
tropic14mo ago
I am trying this method
protected void preview_Callback(object sender, CallbackEventArgsBase e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{

byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{
// Assume that 'imageBytes' is your byte array containing the image data

// Convert the byte array to a Base64-encoded string
string base64String = Convert.ToBase64String(fileBytes);

// Generate the data URL with the Base64-encoded image
string imageUrl = $"data:image/png;base64,{base64String}";

// Use window.location to display the image in a new window or tab
ScriptManager.RegisterStartupScript(this, this.GetType(), "openImage", $"window.open('{imageUrl}')", true);

_name = "View/Attachment/PdfFile.aspx";
}

// cppreview.JSProperties["cpResult"] = _name;
}
}
}
protected void preview_Callback(object sender, CallbackEventArgsBase e)
{
using (var uow = new OriginationUow())
{
var id = new Guid(Convert.ToString(hdf_attchmentid.Get("attachmentid")));
var file = uow.Attachments.GetAll().Where(o => o.InvoiceId == (id));

foreach (var item in file)
{

byte[] fileBytes = item.FileDetails;
var filename = item.DocName;

if (filename.EndsWith(".pdf"))
{
// Assume that 'imageBytes' is your byte array containing the image data

// Convert the byte array to a Base64-encoded string
string base64String = Convert.ToBase64String(fileBytes);

// Generate the data URL with the Base64-encoded image
string imageUrl = $"data:image/png;base64,{base64String}";

// Use window.location to display the image in a new window or tab
ScriptManager.RegisterStartupScript(this, this.GetType(), "openImage", $"window.open('{imageUrl}')", true);

_name = "View/Attachment/PdfFile.aspx";
}

// cppreview.JSProperties["cpResult"] = _name;
}
}
}
Nope, which is not helpful it puts the image there but its just black So i guess it doesnt see it as an issue
Mayor McCheese
Mayor McCheese14mo ago
Are the databases the same?
tropic
tropic14mo ago
Yes they are
Mayor McCheese
Mayor McCheese14mo ago
Hmm was thinking maybe it's just bad in prod
tropic
tropic14mo ago
Which is why I am VERY confused, I was like maybe the DB just got corrupted
Mayor McCheese
Mayor McCheese14mo ago
Btw just glancing at code, I'm concerned about rce Remote code execution
tropic
tropic14mo ago
yeah, honestly was just trying to get something working and then move on If you had to display an image from a Byte[] in aspx how would you go about it, no code needed just a general overview
Mayor McCheese
Mayor McCheese14mo ago
I'd use an ashx 🙂
tropic
tropic14mo ago
Darnit, thats gonna be a bit weird in this case
Mayor McCheese
Mayor McCheese14mo ago
Iirc there is a webforms image control that can take a byte source, but I'm a decade out of date on webforms
tropic
tropic14mo ago
hmmm
Mayor McCheese
Mayor McCheese14mo ago
Basically what you're doing now but with an image control https://stackoverflow.com/questions/39338359/asp-net-display-image-from-byte-array
string base64String = Convert.ToBase64String(arr, 0, arr.Length);
img.Src = "data:image/jpg;base64," + base64String;
string base64String = Convert.ToBase64String(arr, 0, arr.Length);
img.Src = "data:image/jpg;base64," + base64String;
That gets rid of all your window location and calling out specific views in, I'm assuming JavaScript.
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.