How to save image from rich text editor - asp.net

I have a form including rich text editor in it.
While with only content in richtext editor I don't get any problem in saving it in database.
But when I attach an image in it, It always pokes me with this error-
Invalid URI: The Uri string is too long.
Yes the URI is absolutely so long, But why don't this form saves it even if I had given datatype to nvarchar(MAX) to be saved.
See in pictures-
and URI of this image -
While model type has nvarchar(MAX) datatype for this rich text editor.
I am performing simple save function and rendering rich text editor's values by
#Html.Raw() helper.
For this editor's rendering I am doing this on client side-
#Html.Raw(Model.businessDetails)
Model-
public string businessDetails{get;set;}
Why SQL SERVER doesn't save this long uri? What went wrong in my form submission?

first thing first,
you should be doing this as a POST request not as a GET!
after that you have two options
1)
set an attribute so it will allow HTML in the string and it will store the image as base64 in the html and you won't have to worry about storing it anywhere.
[AllowHtml]
public string businessDetails{ get; set; }
2)
grab the src of the image
remove the data:image/png;base64, from it
decode the result with base64 converter , the resulting is binary result
byte[] imageArr ;
//set your imageArr here---
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.CacheOption = BitmapCacheOption.Default;
bi.StreamSource = new MemoryStream(imageArr);
bi.EndInit();
Image img = new Image(); //Image control of wpf
img.Source = bi;
save image to destination. img.Save("path",ImageFormat.Jpeg)
replace the src of that image with an actual URL

Related

NetSuite Advanced PDF Dynamically Created in Script - Cannot Set <img> tag

I am dynamically creating an Advanced PDF in script. I've created an XML string that I am then passing to NetSuite's XML to PDF API; nlapiXMLToPDF(xmlString).
I've added saved searches, tables, styling, and the xml string is parsing correctly.
I cannot add a logo in an tag as I'm not sure how to drill into the file cabinet and store the 'src' of the image.
Has anyone had experience dynamically creating Advanced PDFs in NetSuite and pulling in the logo in a script?
Are you trying to include an image from the file cabinet? If you have the internal ID of the file in the variable fileID, then you can use the following code:
var imageURL = nlapiLoadFile(fileID).getURL();
imageURL = nlapiEscapeXML(imageURL);
var xmlString = ... + '<div><img height="XXpx" width="XXpx" src="'+logoURL+'" /></div>' + ...;
var myPDF = nlapiXMLToPDF(xmlString);
If you want to use the Form Logo set on the Company Information page, then you can populate fileID using the following code:
var companyInfo = nlapiLoadConfiguration('companyinformation');
var fileID = companyInfo.getFieldValue('formlogo');
Then use the first code block to include the logo in xmlString.

Livecode - HTTP POST request

I'm trying to send a HTTP POST request in Livecode to a webservice.
This is the code in Livecode:
put 8 into tId
put the text of field "image_text" into tBlop
put tBlop into field "image_text"
put "id=" & tId & "&blop=" & base64Encode(tBlop) & "&pic=" &tBlop into tArgList
post tArgList to URL "http://www.example.com/api_donation/blop2"
put it into tFormResults
The webservice is written in PHP using the Slim framework
$id = $app->request->post('id');
$blop = $app->request->post('blop');
$pic = $app->request->post('pic');
// decode image
$blop = base64_decode($blop);
$db = getDB();
$sql = "INSERT INTO picture (id, blop, pic) VALUES ($id, $blop, $pic)";
$stmt = $db->prepare($sql);
//execute sql statement
$res = $stmt->execute($data);
However when the image is inserted in the db and I try to get it in Livecode, the image stored in the db is not a valid image.
Is the reason for this error the encoding? Do I need to change some settings in the db (e.g. utf-8)?
Thank you for your help!
The properties imageData (binary) or text (ASCII) of an image are livecode properties regarding what is shown on the screen. They aren't bmp, jpg, png or any other format.
If you need to create a valid format, you need the export command.
See http://livecode.wikia.com/wiki/Export

Binding Image in ASP.NET MVC4 from a byte array

I am binding an image from a byte array in MVC4 view , The byte array is returned from a WEB API which I need to bind it to a view .
Below is the code
string str="JVBERi0xLjUNJeLjz9MNCjQgMCBvYmoNPDwvRSA0NDc1L0ggWyA3NjMgMTI1IF0vTCA1MTczL0xpbmVhcml6ZWQgMS9OIDEvTyA2L1QgNDcwMj4+DWVuZG9iag0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIA0xNCAwIG9iag08PC9EZWNvZGVQYXJtcyA8PC9Db2x1bW5zIDQvUHJlZGljdG9yIDEyPj4vRmlsdGVyIC9GbGF0ZURlY29kZS9JRCBbKG5cMzY0XGZcMjM2XDM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N0cmVhbQp4nGNiAAImRsFqIPG+jYEJyGMEABJTAg0KZW5kc3RyZWFtDWVuZG9iag10cmFpbGVyDTw8L1NpemUgNA0vSUQgWyhuXDM2NFxmXDIzNlwzMjVcMjYyfVwyNjRcMjcwXDM1MCpcMTc3XDAzN1wzNjdcMDA3XDMxNikgKG5cMzY0XGZcMjM2XDMyNVwyNjJ9XDI2NFwyNzBcMzUwKlwxNzdcMDM3XDM2N1wwMDdcMzE2KV0+Pg1zdGFydHhyZWYNMTgyDSUlRU9GDQ==";
Byte[] image = GetBytes(str);
{
Model.ImageData = image;
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
in view Bind it as
<img src="data:image;base64,#System.Convert.ToBase64String(Model.ImageData)" />
The image is not rendering , it is showing an image icon only in page .
Where it is going wrong .Please suggest .
Your str looks like it is already base64, so you don't need to dance to binary and back. You can just stick it in the view where it should be.
Decoding that string from base64 revealed that it is not an image, it's a pdf file.
So your api (3d party, I guess) serves you not an image.
And lastly to successfully use an image like that you probably need to specify mime type like this <img src="data:image/png;base64,some text representation of binary data" />
http://en.wikipedia.org/wiki/Data_URI_scheme

Images are getting published with TCM id appended with the image name

Mode of publishing - static
I'm trying to publish images, but the issue is whenever I publish those images, their TCM URI is appended to their name (i.e if image name is example and its TCM URI is like tcm:1-115, image filename becomes example_tcm1-115).
I have written the following code:
public void Transform(Engine engine, Package package)
{
Filter MMCompFilter = new Filter();
MMCompFilter.Conditions["ItemType"] = Tridion.ContentManager.ItemType.Component;
Folder folder = engine.GetObject("tcm:1-1-2") as Folder;
foreach (Component MMcomp in folder.GetItems(MMCompFilter))
{
Binary binary = engine.PublishingContext.RenderedItem.AddBinary(MMcomp);
String binaryurl = binary.Url;
char[] array = binaryurl.ToCharArray();
Array.Reverse(array);
string obj = new string(array);
string final = newImagepath(obj);
char[] array2 = final.ToCharArray();
Array.Reverse(array2);
string obj2 = new string(array2);
package.PushItem("Image", package.CreateHtmlItem(obj2));
}
public string newImagepath(string filePath)
{
int formatIndex =filePath.IndexOf(".");
string format= filePath.Substring(0,formatIndex);
int finalPath=filePath.IndexOf("_");
string newPath=filePath.Substring((finalPath+1));
return (format+"."+newPath);
}
}
I want to publish images without the TCM URI appended to it. Plz suggest how can it be done.
Chris Summers wrote a very nice article on this very topic http://www.urbancherry.net/blogengine/post/2010/02/09/Unique-binary-filenames-for-SDL-Tridion-Multimedia-Components.aspx
It is basically a very simple thing to fix, but can have huge consequences which you should be aware of!
You can only publish a binary with a certain file-name in a single location once (and a binary can only be published to a single location on the presentation server, unless you publish it as a variant). However, in the CMS it is very easy to create Multimedia Components with the same binary file-name in different folders, which if they get published to the same location will be in conflict. That is why by default SDL Tridion appends the TCM URI to the filename to make it unique.
Simplest is always best.
In your TBB, just push the individual images to the package:
package.PushItem(package.CreateMultimediaItem(component.Id));
Then use the "PublishBinariesInPackage" TBB to publish these images to your presentation server.
You can use the RenderedItem.AddBinary method for this goal. Some of the overloaded versions of the method allows to publish an image as a stream, and pass any file name. For example:
public Binary AddBinary(
Stream content,
string filename,
string variantId,
string mimeType
)

An image from byte to optimized web page presentation

I get the data of the stored image on database as byte[] array;
then I convert it to System.Drawing.Image like the code shown below;
public System.Drawing.Image CreateImage(byte[] bytes)
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes);
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
return image;
}
(*) On the other hand I am planning to show a list of images on asp.net pages as the client scrolls downs the page. The more user gets down and down on the page he/she does see the more photos. So it means fast page loads and rich user experience. (you may see what I mean on www.mashable.com, just take care the new loads of the photos as you scroll down.)
Moreover, the returned imgae object from the method above, how can i show it in a loop dynamically using the (*) conditions above.
Regards
bk
Well, I think the main bottleneck is actually hitting the database each time you need an image. (Especially considering many users accessing the site.)
I would go with the following solution:
Database will store images with the original quality;
.ashx handler will cache images on the file system in various needed resolutions (like 32x32 pixels for icons, 48x48 for thumbnails, etc.) returning them on request and accessing database only once; (in this example is shown how to return an image via ashx handler)
The actual pages will point to .ashx page to get an image. (like <img scr="GetImage.ashx?ID=324453&Size=48" />)
UPDATE:
So the actual workflow in the handler will be like:
public void ProcessRequest (HttpContext context)
{
// Create path of cached file based on the context passed
int size = Int32.Parse(context.Request["Size"]);
// For ID Guids are possibly better
// but it can be anything, even parameter you need to pass
// to the web service in order to get those bytes
int id = Int32.Parse(context.Request["Id"]);
string imagePath = String.Format(#"images/cache/{0}/{1}.png", size, id);
// Check whether cache image exists and created less than an hour ago
// (create it if necessary)
if (!File.Exists(imagePath)
|| File.GetLastWriteTime(imagePath) < DateTime.Now.AddHours(-1))
{
// Get the file from the web service here
byte[] imageBytes = ...
// Save as a file
using (var memoryStream = new MemoryStream(imageBytes))
using (var outputStream = File.OpenWrite(imagePath))
Image.FromStream(memoryStream).Save(outputStream);
}
context.Response.ContentType = "image/png";
context.Response.WriteFile(imagePath);
}

Resources