为什么 text/plain 下载图像类型
Why text/plain downloading image types
我有一个 asp.net 网络服务,如下所示,用于返回 base 64:
[WebMethod]
public string DownloadMediaFiles(string filePathUri, string fileName) {
try {
using(WebClient client = new WebClient()) {
Byte[] bytes = client.DownloadData(filePathUri);
string base64String = Convert.ToBase64String(bytes);
return base64String;
}
} catch (Exception ex) {
throw ex;
}
}
下面是我的 ajax 电话:
$.ajax({
type: 'POST',
url: "Path To My Webservice" + "DownloadMediaFiles",
data: {
filePathUri: filePath,
fileName: 'a.txt'
},
contentType: 'application/json;charset=uf=8',
dataType: 'json',
success: function(data) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;base64,' +
data.d);
element.setAttribute('download', 'a.txt');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
error: function() {
alert("error");
}
});
它工作正常并正在下载文本文件,因为我将 dataUri 设置为数据:text/plain;base64 但是当我提供文件名 'pointing_2017_07_17_03_17_60.png' 这是一个图像文件时,它也在下载尽管 datauri 设置为 text/plain.
下面是我的 html 标记的 txt 文件和图像文件:
txt 文件:
<a href="data:text/plain;base64,certainbase64data" download="a.txt" style="display: none;"></a>
图片文件:
<a href="data:text/plain;base64,certainbase64data" download="pointing_2017_07_17_03_17_60.png" style="display: none;"></a>
href="data:text/plain;base64
此属性正在下载文本和图像文件。
我想知道为什么 text/plain 同时下载图片和文本文件?
text/plain
是文本文件的默认值。即使它真的意味着未知的文本文件,浏览器也会假定它们可以显示它。
Note that text/plain does not mean any kind of textual data. If they
expect a specific kind of textual data, they will likely not consider
it a match. Specifically if they download a text/plain file from a
element declaring a CSS files, they will not recognize it as a
valid CSS files if presented with text/plain. The CSS mime type
text/css must be used.
更多详情:MIME types
因此,您的 pointing_2017_07_17_03_17_60.png 文件也被视为文本文件。
我有一个 asp.net 网络服务,如下所示,用于返回 base 64:
[WebMethod]
public string DownloadMediaFiles(string filePathUri, string fileName) {
try {
using(WebClient client = new WebClient()) {
Byte[] bytes = client.DownloadData(filePathUri);
string base64String = Convert.ToBase64String(bytes);
return base64String;
}
} catch (Exception ex) {
throw ex;
}
}
下面是我的 ajax 电话:
$.ajax({
type: 'POST',
url: "Path To My Webservice" + "DownloadMediaFiles",
data: {
filePathUri: filePath,
fileName: 'a.txt'
},
contentType: 'application/json;charset=uf=8',
dataType: 'json',
success: function(data) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;base64,' +
data.d);
element.setAttribute('download', 'a.txt');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
error: function() {
alert("error");
}
});
它工作正常并正在下载文本文件,因为我将 dataUri 设置为数据:text/plain;base64 但是当我提供文件名 'pointing_2017_07_17_03_17_60.png' 这是一个图像文件时,它也在下载尽管 datauri 设置为 text/plain.
下面是我的 html 标记的 txt 文件和图像文件:
txt 文件:
<a href="data:text/plain;base64,certainbase64data" download="a.txt" style="display: none;"></a>
图片文件:
<a href="data:text/plain;base64,certainbase64data" download="pointing_2017_07_17_03_17_60.png" style="display: none;"></a>
href="data:text/plain;base64
此属性正在下载文本和图像文件。
我想知道为什么 text/plain 同时下载图片和文本文件?
text/plain
是文本文件的默认值。即使它真的意味着未知的文本文件,浏览器也会假定它们可以显示它。
Note that text/plain does not mean any kind of textual data. If they expect a specific kind of textual data, they will likely not consider it a match. Specifically if they download a text/plain file from a element declaring a CSS files, they will not recognize it as a valid CSS files if presented with text/plain. The CSS mime type text/css must be used.
更多详情:MIME types
因此,您的 pointing_2017_07_17_03_17_60.png 文件也被视为文本文件。