Free Support Forum - aspose.com

insertHTML with IMG tag

I am using documentBuilder.insertHTML to insert an HTML string into a document. The HTML string includes an <IMG tag.

When the URL for the image is "localhost," the image displays correctly in the document. When the url is a remote host, the image does not display. It is replaced with a small box containing an x.

How do I get a remote image to display correctly? The image displays fine in IE or Outlook, just not a document created from Aspose.

I am using version 4. Thanks for your help.

Hi,

We are using the standard WebRequest/WebResponse approach to retrieve remote images:

WebRequest request = WebRequest.Create(uri);

WebResponse response = request.GetResponse();

MemoryStream dstStream = new MemoryStream((int)response.ContentLength);

using (Stream responseStream = response.GetResponseStream())

CopyStream(responseStream, dstStream);

Could you please run a similar code on your machine (specifying the proper URL string) and see if responseStream contains valid image data?

Alternatively post or attach your HTML fragment so we can test with it, because with remote URLs works okay in our tests.

Hi, Dmitry. It appears the remote server is seeking authentication. Thanks for the quick reply and the code pointing to the solution.

Dmitry and / or romank, this leads to a follow-up question. The problem was with proxy authentication. Is there anything in the Aspose suite which would enable us to authenticate to a proxy before opening the Word document with the URL?

Thanks.

I have logged your request as #1339. We will see what could be done and reply to you once the things get clear. Thank you for your posting!

So is it a proxy or the remote server seeking authentication?

I guess it is not hard to allow credentials both for remote server and proxy to be specified, thanks the .NET WebRequest class has Credentials and Proxy properties. It is just a question of finding an appropriate place for them in the Aspose.Words API. It is also interesting this is only the first time this feature is being requested.

Do you have any ideas how you want to be able to pass the credentials in?

Credentials might be needed in the following places in Aspose.Words:

  1. DocumentBuilder.InsertImage(string). If you pass a URL, it might be to a server requring authentication and therefore it might be useful to pass the credentials too.
  2. DocumentBuilder.InserHTML. Same as above.
  3. Document - load HTML. Same as above.
  4. ImageData.SetImage(string). Same as above.
  5. Insert an image during mail merge. Same as above.
  6. ImageData.ToStream(). If the image is linked and not stored in the document, it is retrieved and saved to a memory stream and then returned. Retrieving the image might require authentication.
  7. Export to HTML or PDF. Same as above (if the image is linked and not stored in the document, it is retrieved and saved).

I could add Document.Credentials property, but also need Document.Proxy? to allow for HTTP proxy authentication. But that does not help when one needs to load a complete document from HTML using Document ctor. Create more overloads for the ctor?