I was trying to save a file into indexed db and download the same from indexed db once the system is offline. Am facing with two issue.
- The file which i pushed to db after downloading am not able to open the file.
- When large file such like anything more than 150 MB its breadking giving error in JSON.stringify()
Putting my code below. Please help me am in desperate to finish this. is there anything am doing wrong any other better approach or even any leads to get it correct will be relay helpful
downloadfile() {
var self = this;
var getComment = 'http://localhost/PWAS/CommentService/api/Products/GetbookFor?format=xlsx';
this.httpservice.get(getComment).then(function (response) {
self.saveData(response.data, "sampleFile.xlsx");
});
}
saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
// a.style = "display: none";
return function (data, fileName) {
var json = JSON.stringify(data),
blob = new Blob([json], { type: "octet/stream" }),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
when am online am getting the file and putting in to indexed db. am using http interceptor
'response': function (response) {
if (response.config.method == 'GET' && isAUserDataCall(response.config.url)) {
if (navigator.onLine) {
if (isdocumentDownload(response.config.url)) {
var data = { value: JSON.stringify(response) };
var key = response.config.url;
update('OfflineDB', 'UserData', data, key);
}
else {
var data = { value: JSON.stringify(response.data) };
var key = response.config.url;
update('OfflineDB', 'UserData', data, key);
}
}
}
return response;
},
function update(dbName, storeName, data, key,isdirectData) {
var openRequest = openDB(dbName, storeName);
openRequest.onsuccess = function (e) {
console.log("Success!");
db = e.target.result;
updateUserDataResponse(storeName, data, key,isdirectData);
}
openRequest.onerror = function (e) {
console.log("Error");
console.dir(e);
}
}
function updateUserDataResponse(storeName, data, key, isdirectData) {
selectfromDBbyKey(storeName, key).then(function (response) {
var transaction = db.transaction([storeName], "readwrite");
var store = transaction.objectStore(storeName);
if (isdirectData)
var request = store.put(data, key);
else
var request = store.put(data.value, key);
request.onerror = function () {
console.log("Error");
}
request.onsuccess = function () {
console.log("Yolo! Did it");
}
});
}
My Service to download the is
[HttpGet]
public HttpResponseMessage GetbookFor(string format)
{
HttpResponseMessage result = null;
var localFilePath = HttpContext.Current.Server.MapPath("~/App_Data/Comments.pdf");
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// Serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "Comments.pdf";
}
return result;
}
public class eBookResult : IHttpActionResult
{
string PdfFileName;
HttpRequestMessage httpRequestMessage;
HttpResponseMessage httpResponseMessage;
public eBookResult(MemoryStream data, HttpRequestMessage request, string filename)
{
httpRequestMessage = request;
PdfFileName = filename;
}
Task<HttpResponseMessage> IHttpActionResult.ExecuteAsync(CancellationToken cancellationToken)
{
MemoryStream bookStuff = new MemoryStream();
httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK);
httpResponseMessage.Content = new StreamContent(bookStuff);
//httpResponseMessage.Content = new ByteArrayContent(bookStuff.ToArray());
httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = PdfFileName;
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
// Thread.Sleep(200000);
return System.Threading.Tasks.Task.FromResult(httpResponseMessage);
}
}
来源:https://stackoverflow.com/questions/52895405/storing-big-file-pdf-xlsx-into-indexeddb-and-downloading-from-there