Go-Ethereum

在 IPFS Infura 上上傳文件

  • March 11, 2022

我正在嘗試製作一個 dapp 來在 IPFS Infura 上上傳文件。我通過包含腳本在網路瀏覽器中使用 IpfsHttpClient

<script src="https://unpkg.com/ipfs-http-client/dist/index.min.js"></script>

但是當我使用以下方法獲取實例時:

var ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001', { protocol: 'http' });

我收到錯誤和 400(錯誤請求)

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.

當我使用時:

var ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001', { protocol: 'https' });

我得到錯誤:

Uncaught ReferenceError: https is not defined

我該怎麼辦?


我面臨與Infura IPFS CORS非常相似的問題。這是一個最小的程式碼範例:

<!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/ipfs-http-client@30.1.3/dist/index.js"></script>
   <script src="https://bundle.run/buffer@5.2.1"></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>

似乎您的 NO已在'Access-Control-Allow-Origin'問題中得到解決 。看看這是否通過使用Cors proxy解決了您的錯誤。Bad Request

IPFS 使用http協議在您的本地獲取文件,因此您{protocol: 'https'}將引發錯誤。

希望這可以幫助!

現在排序:https ://ethereum.stackexchange.com/a/70120/2524

與原始問題中的片段的區別:{ protocol: 'https' }

未擷取的 ReferenceError:未定義 https

好像缺少'https'引號😇

<!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/ipfs-http-client@30.1.3/dist/index.js"></script>
   <script src="https://bundle.run/buffer@5.2.1"></script>
 </head>
 <body>

   <h3>readAsArrayBuffer to Buffer to Infura to IPFS</h3>

   <input type="file" id="upload">

   <div id="link"></div> <!-- markup created after upload -->

   <script>
       const ipfs = window.IpfsHttpClient('ipfs.infura.io', '5001', { protocol: 'https' });

       $("#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, result) => {
                   console.log(err, result);

             let ipfsLink = "<a href='https://gateway.ipfs.io/ipfs/" + result[0].hash + "'>gateway.ipfs.io/ipfs/" + result[0].hash + "</a>";
             document.getElementById("link").innerHTML = ipfsLink;

               })
           }
           reader.readAsArrayBuffer(this.files[0]);
       })
   </script>

 </body>
</html>

引用自:https://ethereum.stackexchange.com/questions/68947