我尝试使用 Tizen 下载 API 从服务器下载媒体(图像、视频、GIF 等),但它无法正确下载文件。这是我从 tizen 文档中使用的代码:
const downloadUrl = 'https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg';
const downloadFileName = 'wi-night-partly-cloudy.jpg';
var downloadRequest = new tizen.DownloadRequest(downloadUrl, path, downloadFileName, 'ALL', {'Content- Type' : 'image/jpeg'});
tizen.systeminfo.getPropertyValue('NETWORK', function(networkInfo) {
if (networkInfo.networkType === 'NONE') {
console.log(`Network connection is not available.
Download is not possible.`);
downloadRequest = null;
}
});
var listener = {
/* When the download progresses (interval is platform-dependent) */
onprogress: function(id, receivedSize, totalSize) {
console.log('Received with id: ' + id + ', ' + receivedSize + '/' + totalSize);
},
/* When the user pauses the download */
onpaused: function(id) {
console.log('Paused with id: ' + id);
},
/* When the user cancels the download */
oncanceled: function(id) {
console.log('Canceled with id: ' + id);
},
/* When the download is completed */
oncompleted: function(id, fullPath) {
console.log('Completed with id: ' + id + ', full path: ' + fullPath);
},
/* When the download fails */
onfailed: function(id, error) {
console.log('Failed with id: ' + id + ', error name: ' + error.name);
}
};
let downloadId = tizen.download.start(downloadRequest, listener);
tizen.download.getState(downloadId);
var state = tizen.download.getState(downloadId);
console.log(state);
var downloadRequest = tizen.download.getDownloadRequest(downloadId);
console.log(downloadRequest);
var MIMEType = tizen.download.getMIMEType(downloadId);
console.log(MIMEType);
当我尝试在启动方法之前 console.log 下载请求时,这就是我得到的:
这是启动方法之后的下载请求和 mime 类型,注意:我明确将内容类型设置为 image/jpeg:
此外,下载也没有问题。它一直说下载完成,并将文件存储在确切的位置,但该文件不是图像(在本例中,我尝试下载图像):
我尝试使用 Tizen 下载 API 从服务器下载媒体(图像、视频、GIF 等),但它无法正确下载文件。这是存储到以下位置的文件输出:
注意:当我尝试使用浏览器或邮递员从同一 URL 获取图像时,它工作正常,我得到了具有正确尺寸和内容类型的图像。
有人遇到过这个问题吗?
我无法在我的设置中重现您的问题。
我鼓励您提供有关如何使用完整代码执行所有步骤的更多详细信息。目前很难确定可能出现的问题,因为您没有提供会生成屏幕截图上显示的无效结果的代码。
以下是我尝试按照您的步骤操作的方法:
const downloadUrl = 'https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg';
const downloadFileName = 'wi-night-partly-cloudy.jpg';
const path = 'downloads';
function printRequest(downloadId, prefix) {
var state = tizen.download.getState(downloadId);
var downloadRequest = tizen.download.getDownloadRequest(downloadId);
try { var MIMEType = tizen.download.getMIMEType(downloadId); } catch (e) { }
console.log(prefix + " - Request: " + downloadId + ": " + JSON.stringify(downloadRequest) + ", state: " + state + ", mime type: " + MIMEType);
}
var downloadRequest = new tizen.DownloadRequest(downloadUrl, path, downloadFileName, 'ALL', { 'Content- Type': 'image/jpeg' });
tizen.systeminfo.getPropertyValue('NETWORK', function (networkInfo) {
if (networkInfo.networkType === 'NONE') {
console.log(`Network connection is not available.
Download is not possible.`);
downloadRequest = null;
}
});
var listener = {
/* When the download progresses (interval is platform-dependent) */
onprogress: function (id, receivedSize, totalSize) {
//console.log('Received with id: ' + id + ', ' + receivedSize + '/' + totalSize);
printRequest(id, "onprogress");
},
/* When the user pauses the download */
onpaused: function (id) {
console.log('Paused with id: ' + id);
},
/* When the user cancels the download */
oncanceled: function (id) {
console.log('Canceled with id: ' + id);
},
/* When the download is completed */
oncompleted: function (id, fullPath) {
printRequest(id, "oncompleted");
console.log('Completed with id: ' + id + ', full path: ' + fullPath);
},
/* When the download fails */
onfailed: function (id, error) {
console.log('Failed with id: ' + id + ', error name: ' + error.name);
}
};
let downloadId = tizen.download.start(downloadRequest, listener);
printRequest(downloadId, "After start");
我在开始前、开始后(同步)、下载期间和下载完成后打印了下载请求和 MIME 类型。
After start - Request: 9: {"url":"https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg","destination":"downloads","fileName":"wi-night-partly-cloudy.jpg","networkType":"ALL","httpHeader":{"Content- Type":"image/jpeg"},"verifyHost":true}, state: QUEUED, mime type: undefined
onprogress - Request: 9: {"url":"https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg","destination":"downloads","fileName":"wi-night-partly-cloudy.jpg","networkType":"ALL","httpHeader":{"Content- Type":"image/jpeg"},"verifyHost":true}, state: DOWNLOADING, mime type: image/jpeg
onprogress - Request: 9: {"url":"https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg","destination":"downloads","fileName":"wi-night-partly-cloudy.jpg","networkType":"ALL","httpHeader":{"Content- Type":"image/jpeg"},"verifyHost":true}, state: COMPLETED, mime type: image/jpeg
oncompleted - Request: 9: {"url":"https://magic-sign.cloud/v4/web/MSlibrary/wi-night-partly-cloudy.jpg","destination":"downloads","fileName":"wi-night-partly-cloudy.jpg","networkType":"ALL","httpHeader":{"Content- Type":"image/jpeg"},"verifyHost":true}, state: COMPLETED, mime type: image/jpeg
Completed with id: 9, full path: /opt/usr/home/owner/content/Downloads/wi-night-partly-cloudy.jpg
这样看来,每一步都正确设置了 mime 类型。 我还通过 sdb shell 确认该文件是有效的:
# file --mime-type -b wi-night-partly-cloudy.jpg
image/jpeg
如果我的方法不适合您,请提供有关您使用哪个 Tizen 版本以及您的设置中使用哪种电视设备的更多详细信息。