我试图将文件保存到索引数据库中,并在系统脱机后从索引数据库下载该文件。面临两个问题。
将我的代码放在下面。请帮我拼命完成这个任务。是否有任何其他任何更好的方法做错了,甚至任何导致它正确的线索都将是有益的
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);
};
}());
在线时正在获取文件并放入索引数据库。我正在使用http拦截器
'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");
}
});
}
我的服务是下载
[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);
}
}