You need to sign in to do that
Don't have an account?
![barak inbal 10 barak inbal 10](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Fetch image from Rich Text Field via Https
I have C# batch which communicate with salesforce objects via SOAP API.
I want to fetch an image which is part of the Rich Text Field in salesforce custom object.
The rich text field himself is Html text and I can get the url of the image tag, but the problem is that the url himself is Https = Http secure connection:
The url of the image tag:
https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I (http://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I)
As a result of this, I can't get the resource and write it to a file as a local image for later use.
This is the C# code for fetching the image:
I know that the secure url using java servlet for exposing the image to the world.
The problem is that I can't get the image data and always get the following respond:
Any suggestion how to get the actual image data from the Rich Text Area field?
I want to fetch an image which is part of the Rich Text Field in salesforce custom object.
The rich text field himself is Html text and I can get the url of the image tag, but the problem is that the url himself is Https = Http secure connection:
The url of the image tag:
https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I (http://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I)
As a result of this, I can't get the resource and write it to a file as a local image for later use.
This is the C# code for fetching the image:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); bool answer = response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase); if ((response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)&& response.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase)){ // if the remote file was found, download it using (Stream inputStream = response.GetResponseStream()) using (Stream outputStream = File.OpenWrite(filepath)) { byte[] buffer = new byte[8192]; int bytesRead; logger.InfoFormat("Writing to file, filepath:{0}", filepath); do { bytesRead = inputStream.Read(buffer, 0, buffer.Length); logger.InfoFormat("bytesRead: {0} ", bytesRead); outputStream.Write(buffer, 0, bytesRead); } while (bytesRead != 0); //TODO outputStream.Close(); } return true; } } catch (Exception ex) { logger.ErrorFormat("Failed to download {0} to {1} : {2}", uri, filepath, ex.Message); logger.Debug("Failed to download " + uri, ex); } return false;
I know that the secure url using java servlet for exposing the image to the world.
The problem is that I can't get the image data and always get the following respond:
<script> if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I'); } else if (window.location.replace){ window.location.replace('https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I'); } else {; window.location.href ='https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I'; } </script>
Any suggestion how to get the actual image data from the Rich Text Area field?
I can stream - read a text file with absolutely no problem using somewhat similar code as yours.