Closed zhgromov closed 4 years ago
It's some wierd behavior. I get the same thing.
But if I try :
for await (const file of await ipfs.add({
path: 'hello.txt',
content: 'Hello World 101'
})) {
console.log(JSON.stringify(file));
}
This works for me and I get :
{"path":"hello.txt","cid":{"codec":"dag-pb","version":0,"hash":{"type":"Buffer","data":[18,32,138,210,135,65,209,1,129,203,63,121,61,77,238,44,52,218,162,227,202,125,215,181,195,134,251,174,21,62,164,24,241,64]}},"size":23,"mode":420}
I know it's a for loop for just an element. But it's the only way i got it working. Hope it helps as a workaround.
sorry, how I can get hash address from this data? I saved address in my database and next view for hash.
"data":[18,32,138,210,135,65,209,1,129,203,63,121,61,77,238,44,52,218,162,227,202,125,215,181,195,134,251,174,21,62,164,24,241,64
Assuming you have file
as a result, you can just do file.cid.toString()
. This should return the address of the file.
thank you. if I use different file (not txt), I should write random hardcode path? Because I haven't path of my file, I just save send jpg from props
something like this
for await (const inputFile of await ipfs.add({ path: 'randompath.txt', content: file })) { return inputFile.cid.toString() }
in props I just send file
{name: "1411957083_w1600x1200_15333.jpg", lastModified: 1583487251496, lastModifiedDate: Fri Mar 06 2020 12:34:11 GMT+0300 , webkitRelativePath: "", size: 462128, …}
and get hash
Qmf6SyXbY4F8YChTGiRu6wai33s9q71dWg4EX5V8n6axwY
it will be correct?
Also this option working on Chrome and crash in safari
Unhandled Rejection (TypeError): null is not an object (evaluating 'ipfs.add')
sorry for questions, I try to understand and fix it..
You can also do it without providing the Path
property.
I don't know what are the issues in the browsers.
One way to test it if the hash is ok is to just just test it with ipfs.get()
The API was updated to use async iterators as of v0.41.0
, please see the migration guide.
Previously ipfs.add
returned an array, which was great, unless you were adding a lot of files in which case your process could run out of memory. Now it returns an async iterator as you've found.
n.b. you don't need to do the double await:
// instead of
for await (const inputFile of await ipfs.add({ path: 'randompath.txt', content: file })) {
// do:
for await (const inputFile of ipfs.add({ path: 'randompath.txt', content: file })) {
I'm glad you figured it out though, in future please ask usage questions like this at https://discuss.ipfs.io so other people can discover the answer.
Hello, guys. Few month ago I add ipfs for upload files, all worked correctly, but a week ago I have problem and can’t resolve it. I try to send file to IPFS and get hash of file. (react)
like this. connect ipfs :
and try to send file
so, when I try to upload, I have error
earlier it was hash of file.
could you help me?