Inserting Images via HTML not working once deployed

I have a website where images are stored in a database and are served up via an ASPX page. I have a page where the user can enter data via a WYSIWYG editor (Telerik RadEditor). When an image is inserted into the editor, the full url path is used for the tag. The user can then click a button to generate a Word document report. The report generator code uses a class that implements IFieldMergingCallback where I call the InsertHtml method. When running locally, the image is included in the report as expected. However, when the code is deployed to a web server, any images in the HTML are not displayed. If I take the image URL and paste it into the web browser toolbar, it loads just fine. Any idea why the image would not be inserted into the document when running from a web server but work just fine when running locally? Any help would be greatly appreciated.

Hi there,

Thanks for your inquiry. I think, by using the following configuration settings in your application will solve your problem. Hope this helps you. Please let us know if you have any more queries.

<configuration>
	<system.net>
		<settings>
			<httpWebRequest useUnsafeHeaderParsing="true" />
		</settings>
	</system.net>
</configuration>
HttpWebRequestElement webRequestSettings = new HttpWebRequestElement();
webRequestSettings.UseUnsafeHeaderParsing = true;

Unfortunatly that did not work. I’ve run some additional tests and found that the page serving up the image never gets called when the report is being generated. My guess is that it might be a security issue since my website requires AD authentication. I’ve made sure that the AD user of the website’s App Pool has permission to view the website, but still no luck.
As a workaround, I’ve figured out that I can:

  1. Insert HTML before any image tags

  2. Insert the image directly using the builder.InsertImage method

  3. Insert HTML after the image tags

This isn’t an ideal solution because it requires string parsing which I would prefer to stay away from, so if you might have any other ideas for me to try, I would be very greatful.
Thanks.

Hi,

Thanks for your inquiry. It seems that the URL you are using in IMG tag has access restrictions at your side. Could you please try the following code snippets at your web server and share your findings with us? Please use your the image URL in the following code snippet.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string html = @"<img src=‘URL…’ alt=‘test image’ width=‘178’ height=‘129’ />";
builder.InsertHtml(html);
doc.Save(@"c:\sample.pdf");
const string href = "your URL/logo.png";
WebRequest request = WebRequest.Create(href);
WebResponse response = request.GetResponse();
using(Stream responseStream = response.GetResponseStream())
using(FileStream fileStream = File.OpenWrite("d:\\logo.png"))
{
    byte[] buf = new byte[4096];
    while (true)
    {
        int bytesRead = responseStream.Read(buf, 0, buf.Length);
        if (bytesRead <= 0)
            break;
        else
            fileStream.Write(buf, 0, bytesRead);
    }
}

The 2nd code block gave me the best feedback:

The remote server returned an error: (401) Unauthorized … at WebRequest.GetResponse()

So what I feared was happening is happening. The server is attempting to retrieve the image under the IIS App Pool identity which doesn’t have access to the website (AD Authentication is required). Any suggestions? This is the first time I’ve had to deal with the server requesting resources directly like this.
Thanks.

Hi,

Thanks for your inquiry.

The second code snippet does not use API of Aspose.Words. So the problem is somewhere at your side. You should contact your system administrator to allow full trust for your applications. Please let us know if you have any more queries about Aspose.Words.

After doing some more research, it looks like Aspose.Words does not pass any credentials with the web request when fetching images. If I take your test code and add a line to use default credentials like this:

WebRequest request = WebRequest.Create(href);
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();

The image is returned when the GetResponse() method is called. So I’m guessing when Aspose.Words encounters an tag and tries to retrieve the image, it isn’t setting any credentials thus causing the GetResponse() call to fail. Is there any workaround to this?

Hi there,

Thanks for your inquiry. Could you please attach your image URL here for testing? I will investigate the issue on my side and provide you more information.

I’m afraid not, it’s on a closed internal network. Here is what I can tell you:

  • The user is in the format /imageviewer.aspx?imageid=#
    • The webpage retrieves the image from the database and streams down the image
  • The website is standard Windows AD Authentication with anonymous access disabled.
  • The website App Pool runs under an AD account, not a built-in system account.

Hi,

Thanks for sharing the detail. In your case, I suggest you please use the DocumentBuilder.InsertImage Method (Stream). This method inserts an image from a stream into the document.

Moreover, you can load a web page into Aspose.Words DOM by using following code snippet. Once you have loaded your web page (html) into Aspose.Words DOM, you can easily use the InsertDocument method to insert a document (in your case the html page) at any place of another document.

Please read following documentation link for the detail of InsertDocument method. Hope this helps you. Please let us know if you have any more queries.
https://docs.aspose.com/words/java/insert-and-append-documents/

string url = @"https://www.aspose.com/";
// Prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
request.UseDefaultCredentials = true;
// Execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
// We will read data via the response stream
Stream resStream = response.GetResponseStream();
// Write content into the MemoryStream BinaryReader resReader = new BinaryReader(resStream);
MemoryStream docStream = new MemoryStream(resReader.ReadBytes((int) response.ContentLength));
Document doc = new Document(docStream);