У меня проблемы с отображением сохраненного файла.После того, как мне удалось сохранить его в моей базе данных, я хочу восстановить его и показать при нажатии кнопки в новом окне или, возможно, в окне / окне предварительного просмотра, когда это возможно.Проблема с моим текущим кодом состоит в том, что я получаю что-то обратно, но изображение не отображается.И я не уверен, что данные поврежденного файла или что-то еще.
C #
public FileContentResult GetDocument(int id)
{
var db = new PetaPoco.Database("umbracoTicketConnectionString");
FileModel file = db.SingleOrDefault<FileModel>("Select * from FileTable where ElementId=" + id);
byte[] doc = file.BinaryFile;
string fileName = file.name;
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
// when sending with JSON then maxJsonLength exception occurs.
return File(doc, MimeMapping.GetMimeMapping(fileName));
}
JS
$scope.openFile = function () {
var id = 1198;
//var config = { responseType: 'blob' , id: 1198 }; // TODO
$http.get("/umbraco/Surface/TicketFormSurface/GetDocument?id="+ id).then(function onSuccess(response) {
console.log(response.data);
var binaryData = [];
binaryData.push(response.data);
var fileURL = URL.createObjectURL(new Blob(binaryData, { type: 'image/jpeg' }))
window.open(fileURL);
});
}
Вывод Console.log:

Решено
Я решил это, изменив код в контроллере js.
$scope.openFile = function () {
var id = 1198;
//var config = { responseType: 'blob' , id: 1198, }; // TODO
$http.get("/umbraco/Surface/TicketFormSurface/GetDocument?id="+ id, {
headers: {
'Content-Type': 'image/jpg'
},
responseType: 'blob'
}).then(function onSuccess(response) {
console.log(response.data);
var binaryData = [];
binaryData.push(response.data);
var fileURL = URL.createObjectURL(new Blob(binaryData, { type: 'image/jpeg' }))
window.open(fileURL);
});
}