I am uploading a file through an API call that requires putting the file inside the Request body and using PUT. Using the following code, I am able to upload something that creates the file and returns a 200 status. The rawData length is exactly the length expected based on the initial file size. However, the files created have some additional bytes and cannot be opened. For example, a jpg file I upload at 234 kb has 352 kb after upload, while a docx file starting at 12 kb has 17 kb after upload.
var contentType = file.type;
var reader = new FileReader();
reader.onload = function (e) {
var rawData = reader.result;
$http({
url: appSettings.serverPath + '/File/' + fileId + '/Content',
method: 'PUT',
headers: { 'Content-Type': undefined },
data: rawData,
transformRequest: []
}).success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (error) {
deferred.reject(error);
});
};
reader.readAsBinaryString(file);
Besides using 'Content-Type' of undefined, I have used the content type of the file, but I have the same issue.
I have tried calling the readAsArrayBuffer method on the FileReader and passing in a Uint8Array of the rawData, but that does not pass in any data on the Request and creates a file with 0 bytes.
I have also tried calling the readAsDataURL method on the FileReader, which encodes the data completely differently such that my rawData and resulting file are 313 kb for the jpg. The file is still corrupted and cannot be opened.
Is there some additional encoding, etc., that I should be doing to send the correct data?
Thanks in advance!!