go 服務(wù)端:
就是先把這個(gè)圖片讀出來(lái) ?然后返回二進(jìn)制的數(shù)據(jù)
byteFile, err := ioutil.ReadFile("." + "/processed/" + uuidStr+"processed.png")
if err != nil {
fmt.Println(err)
}
c.Header("Content-Disposition", "attachment; filename=file-name.txt")
c.Data(http.StatusOK, "application/octet-stream", byteFile)
js 前端獲取圖片:
function doSomething() {
// var videoData = "";
var fileObj = document.getElementById("fileimage").files[0]; // js 獲取文件對(duì)象
var formData = new FormData();
formData.append("file", fileObj); //上傳一個(gè)files對(duì)象
// formData.append("參數(shù)", "參數(shù)1"); //若需要上傳的多個(gè)參數(shù)
// formData.append("參數(shù)", "參數(shù)2");
var url = "http://127.0.0.1:8000/upload/histogram";
$.ajax({
url: url,
// dataType:"text",
// dataType: "json",
type: "post",
data: formData,
processData : false, //不處理發(fā)送的數(shù)據(jù)
contentType: false, //不設(shè)置Content-Type請(qǐng)求頭
xhrFields:{
responseType: 'blob'
},
success: function(res) {
console.log(111)
var img = document.getElementById('img-from-local-storage');
var url = window.URL || window.webkitURL;
img.src = url.createObjectURL(res);
<img id="img-from-local-storage" />
參考文章:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-730358.html
Sending image files from back-end to front-end - JavaScript - The freeCodeCamp Forum文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-730358.html
到了這里,關(guān)于ajax method to retrieve images as a blob的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!