Reputation:
I have a asp.net web service like as below for returning 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;
}
}
And below is my ajax call:
$.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");
}
});
It is working fine and downloading text file as I have set dataUri as data:text/plain;base64 but when I provide file name as 'pointing_2017_07_17_03_17_60.png' which is a image file then also it is downloading although datauri is set to text/plain.
Below is my html mark up for txt file as well as image file:
txt file:
<a href="data:text/plain;base64,certainbase64data" download="a.txt" style="display: none;"></a>
Image file:
<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
this attribute is downloading both text and image files.
I want to know why text/plain is downloading both image and text file?
Upvotes: 1
Views: 1755
Reputation: 7970
text/plain
is the default value for textual files. Even if it really means unknown textual file, browsers assume they can display it.
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.
For more details: MIME types
So, your pointing_2017_07_17_03_17_60.png file is also treated as text file.
Upvotes: 1