I am trying to make a dapp to upload files on IPFS Infura. I am using IpfsHttpClient in web browser by including the script
<script src="https://unpkg.com/ipfs-http-client/dist/index.min.js"></script>
But when I get the instance using:
var ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001', { protocol: 'http' });
I get the error and 400(Bad Request)
Access to fetch at 'http://ipfs.infura.io:5001/api/v0/add?stream-channels=true' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
And when I am using :
var ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001', { protocol: 'https' });
I get the error:
Uncaught ReferenceError: https is not defined
What should I do?
I'm facing a very similar issue with Infura IPFS CORS. Here is a minimal code sample:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Infura IPFS CORS issue</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>
<script src="https://bundle.run/[email protected]"></script> <!-- required to convert ArrayBuffer to Buffer -->
</head>
<body>
<h3>readAsArrayBuffer to Buffer to Infura to IPFS</h3>
<input type="file" id="upload">
<script>
const ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001')
$("#upload").on("change", function() {
var reader = new FileReader();
reader.onload = function (e) {
const magic_array_buffer_converted_to_buffer = buffer.Buffer(reader.result); // honestly as a web developer I do not fully appreciate the difference between buffer and arrayBuffer
ipfs.add(magic_array_buffer_converted_to_buffer, (err, ipfsHash) => {
console.log(err,ipfsHash);
})
}
reader.readAsArrayBuffer(this.files[0]);
})
</script>
</body>
</html>