Hello,
I've been using Power Apps (mainly canvas) and Power Automate for a few years, but I'm fairly new to Power Pages and the Dataverse API.
I need to upload a file to Dataverse from my Power Pages site, then download this file into a canvas application.
The Power Pages application design required me to use HTML and JavaScript in my application. The problem is that I could not upload a file in my Dataverse table, in a file type column, despite the existing documentation...
I have created a file type column, his logical name is "crcb8_file_content"
Here are two of my attempts that send a 500 error :
Attempt 1 :
var reader = new FileReader();
reader.readAsDataURL(file); // from an input file
reader.onload = function (e) {
var encoded = e.target.result.split(',')[1];
webapi.safeAjax({
type: "POST",
url: "/_api/crcb8_productorderstest1s",
contentType: "application/json",
data: JSON.stringify({
"crcb8_name": name,
"crcb8_current_user" : userEmail,
"crcb8_file_content": encoded
}),
success: function (res, status, xhr) {
console.log("Entity ID: " + xhr.getResponseHeader("entityid"));
},
error: function (xhr, status, error) {
console.log("error: "+ error)
}
});
}
Attempt 2 :
webapi.safeAjax({
type: "POST",
url: "/_api/crcb8_productorderstest1s",
contentType: "application/json",
data: JSON.stringify({
"crcb8_name": name,
"crcb8_current_user": userEmail
}),
success: function (res, status, xhr) {
var newRecordId = xhr.getResponseHeader("entityid");
console.log("Entity ID: " + newRecordId + " now attach the file");
// Now attach the file to the entity unsing safeAjax and PATCH
var reader = new FileReader();
reader.readAsDataURL(file); // from input file type
reader.onload = function (e) {
var encoded = e.target.result.split(',')[1];
webapi.safeAjax({
type: "PATCH",
url: "/_api/crcb8_productorderstest1s(" + newRecordId + ")/crcb8_file_content",
contentType: "application/octet-stream",
processData: false,
data: encoded,
success: function (res, status, xhr) {
console.log("File attached");
},
error: function (xhr, status, error) {
console.log("error: " + error)
}
});
}
},
error: function (xhr, status, error) {
console.log("error: " + error)
}
});
Your help would be very useful to me and I thank you in advance!