C
C#2y 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
tropicOP2y 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
tropicOP2y ago
What is generated
tropic
tropicOP2y ago
What is supposed to be there
Mayor McCheese
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
tropicOP2y 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
Is there anything in commit history?
tropic
tropicOP2y 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
Also what are you using in azure
tropic
tropicOP2y ago
App Service So we are working with an app no documentation sadly and no commit history
Mayor McCheese
Seriously? Wtf, I'm so sorry to hear that.
tropic
tropicOP2y ago
Yeah it sucks
Mayor McCheese
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
tropicOP2y ago
Os is windows
Mayor McCheese
Ugh
tropic
tropicOP2y ago
SKU would be the default Basic (B1) Yeah, I know xD
Mayor McCheese
Sku won't help, but I figured on the off chance something doesn't work as expected on Linux with web forms.
tropic
tropicOP2y ago
Yeah
Mayor McCheese
Any log messages?
tropic
tropicOP2y 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
Are the databases the same?
tropic
tropicOP2y ago
Yes they are
Mayor McCheese
Hmm was thinking maybe it's just bad in prod
tropic
tropicOP2y ago
Which is why I am VERY confused, I was like maybe the DB just got corrupted
Mayor McCheese
Btw just glancing at code, I'm concerned about rce Remote code execution
tropic
tropicOP2y 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
I'd use an ashx 🙂
tropic
tropicOP2y ago
Darnit, thats gonna be a bit weird in this case
Mayor McCheese
Iirc there is a webforms image control that can take a byte source, but I'm a decade out of date on webforms
tropic
tropicOP2y ago
hmmm
Mayor McCheese
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
Accord2y 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.
Want results from more Discord servers?
Add your server