-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendFileToBackend.js
119 lines (95 loc) · 3.8 KB
/
SendFileToBackend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function displayImg4 (e){
let reader = new FileReader()
const file = document.getElementById('file');
reader.readAsDataURL(file.files[0])
reader.onload = () => {
console.log("reader.onload");
setImage(reader.result)
}
postImage();
}
async function postImage () {
console.log("image setted: " + image);
const ipfsViewResponse = await fetch("ipfsRegister",
{method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(image)
})
.then(res => res.json())
.then(data => {
console.log("postImage");
console.log(data);
setMetadata({
...metadata,
fileWebLink: data.fileWebLink,
})
return data;
})
console.log(ipfsViewResponse);
}
function displayImg3() {
let file = document.getElementById("file").files[0];
let reader = new FileReader()
reader.onload = async function (e) {
e.preventDefault();
const data = new FormData();
data.append('image', file[0]);
console.log(data);
document.querySelector("#output").src = e.target.result;
await ipfsRegister(data);
document.getElementById("submit_img").style.display = "none";
document.getElementById("file").style.display = "none";
document.querySelector(".metadata-container").style.display = "block"
}
reader.readAsDataURL(file);
}
const ipfsRegister = (blob) => {
console.log(blob);
fetch('ipfsRegister',{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(blob)
})
.then(res => res.json())
.then(data => {
console.log(data);
setMetadata({
...metadata,
fileWebLink: data.url,
});
})
.catch(err => console.log(err));
}
function displayImg(){
const file = document.getElementById('file').files[0];
let link = document.querySelector('#link');
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = async function (e) {
e.preventDefault();
//this is the way i encode the file image into a blob
let blob = new Blob([file], {type: 'image/jpeg'});
link.href = URL.createObjectURL(blob);
//link.click();
//This way i send a html-img
var img = document.getElementById('output');
img.src = e.target.result;
//send to server as a formData - not working
//var formData = new FormData();
//formData.append("blob", blob);
//try to send formData to server
//ipfsRegister(formData);
//Try to send pure Blob to server
//ipfsRegister(blob);
//This way i send a dataURLS.
//ipfsRegister(reader.result);
const encodedUrl = encodeURI(reader.result);
ipfsRegister(encodedUrl);
}
}
//{'adress': 'addr_test1qrhjehlx4dcynd78cf7mlp7cjvvwawty653twdm2m0kw40xnputj70vg2cecpjqj203tw6rtjcng7akmehz393esz0rqp3lhl0', 'title': 'TheLobFather', 'author': 'Marcelismo7', 'description': 'TheLobster King', 'fileWebLink': 'ipfs://QmSvg6Ueut5gfS4hLTYujmhAk9fpz4HVTWVUysybEKsuiM', 'arweaveId': '', 'nsfw': ''}
//{'message': 'Entered the MINT ASSET CNODEJS', 'metadata': {'title': 'TheLobFather', 'author': 'Marcelismo7', 'description': 'TheLobster King', 'fileWebLink': 'ipfs://QmSvg6Ueut5gfS4hLTYujmhAk9fpz4HVTWVUysybEKsuiM', 'arweaveId': '', 'nsfw': ''}, 'adress': 'addr_test1qrhjehlx4dcynd78cf7mlp7cjvvwawty653twdm2m0kw40xnputj70vg2cecpjqj203tw6rtjcng7akmehz393esz0rqp3lhl0'}
//way to user download the file;
<a download={metadata.title+".jpg"} id="link" href={file_url}>{metadata.title}</a>