web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Compress the image and upload to the entity image in Product Entity

(0) ShareShare
ReportReport
Posted on by 233

Hello Experts,

For choosing the image and uploading to the thumbnail in ms crm i have wriiten  Html and javascript , i don't now how to resize the image to 144* 144 pixel to fit exactly in the tumbnail please suggest me below is my html and javascript code.please suggest me i am new to html and javascrpit.

<html><head>
<meta>
<meta>


<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<meta><meta></head>

<body onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;">
<input type="file" id="fileUpload">
<input type="button" id="upload" value="Upload" onclick="Upload(event)">

<script src="sonade_/Scripts/SDK.REST.js" type="text/javascript"></script>

<script src="sonade_ImageUpload.js" type="text/javascript"></script>


</body></html>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var Cnt = 0;
var index = 0;
var id = null;
function Upload() {

debugger;
id = window.parent.Xrm.Page.data.entity.getId();
var fileUpload = document.getElementById("fileUpload");
var ufiles = fileUpload.files;
nfiles = ufiles;
Cnt = ufiles.length;

sendFile();

}

function _arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}

function sendFile() {
if (index < Cnt) {
var reader = new FileReader();
reader.onload = function (evt) {
var str = _arrayBufferToBase64(reader.result);
var tt = 'data:image/png;base64,'
document.getElementById("form_pic_image").src = tt + str;
console.log( nfiles[index].name, nfiles[index].type, '--->1');

SaveImage(str, nfiles[index].name, nfiles[index].type);
}
reader.readAsArrayBuffer(nfiles[index]);
}

}
function SaveImage(docBody, fName, mType) {

var entity = {};
if (docBody != null) {
entity.EntityImage = docBody;
UpdateEntities(id, entity, 'Product');
}
else {
loading.close();
alert('something wrong..!');

}


}
function UpdateEntities(EntityID, EntityFields, EntityName) {
var ContactEntity = {};
ContactEntity = EntityFields;

SDK.REST.updateRecord(
EntityID,
EntityFields,
EntityName,
function () {
alert("Successfully updated.");
//document.getElementById("form_pic_image").style.width = "100%";
//document.getElementById("form_pic_image").style.height = "100%";

},
function (error) {
alert(error.message);
}
);


}

I have the same question (0)
  • SergioMarques Profile Picture
    on at

    Hello Amrutha,

    Your question is going more on the side of JS, so what you can is also post it in other forum more oriented to that purpose.

    As for an answer of what your are going, I think you will need to take advantage of a JS framework to achieve your goal.

    In this StackOverflow article there are some good suggestions: stackoverflow.com/.../how-to-convert-a-high-resolution-image-into-a-lower-one

  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Amrutha,

    You can use Canvas element to resize image file.

    In this demo, I created two image elements, 1 is to save selected image object,  2 is to save resized image object,

    finally my last "Upload image" button will get the part after the comma to update entityimage field of entity.

    Example

    Raw image:

    azure_2D00_bg.jpg

    Resized image:

    5775.jpg

    Page preview:

    pastedimage1583423407754v1.png

    Result in Crm:

    pastedimage1583423434126v2.png

    Full code

    
    
    
        
        Page Title
        
        
        
    
    
    
    
        

    Image resizer:

    Just make some changes to your UpdateEntities function.:)

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans