如何在JavaScript中将可读流转换为Blob?

时间:2019-12-10 12:53:43

标签: javascript node.js fetch multipartform-data

我有一个测试,该测试应读取图像文件并将图像文件提交到接受multipart-formdata的api。

我正在将fetch api和formdata类一起使用来设置图像文件。 formdata仅接受一个blob。因此,在测试中,我必须将读取的流类型为blob的文件转换为blob。

    <?php  
    $connect = mysqli_connect("localhost", "root", "", "datedate");  
    $query = "SELECT * FROM tbl_order ORDER BY order_id asc";  
    $result = mysqli_query($connect, $query);  
    ?>  
    <!DOCTYPE html>  
    <html>  
    <head>  
       <title>Ajax PHP MySQL Date Range Search using jQuery DatePicker</title>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>  
       <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
  </head>  
  <body>  
       <br /><br />  
       <div class="container" style="width:900px;">  
            <h2 align="center">Ajax PHP MySQL Date Range Search using jQuery DatePicker</h2>  
            <h3 align="center">Order Data</h3><br />  
            <div class="col-md-3">  
                 <input type="text" name="from_date" id="from_date" class="form-control" placeholder="From Date" />  
            </div>  
            <div class="col-md-3">  
                 <input type="text" name="to_date" id="to_date" class="form-control" placeholder="To Date" />  
            </div>  
            <div class="col-md-5">  
                 <input type="button" name="filter" id="filter" value="Filter" class="btn btn-info" />  
            </div>  
            <div style="clear:both"></div>                 
            <br />  
            <div id="order_table">  
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="5%">ID</th>  
                           <th width="30%">Customer Name</th>  
                           <th width="43%">Item</th>  
                           <th width="10%">Value</th>  
                           <th width="12%">Order Date</th>  
                      </tr>  
                 <?php  
                 while($row = mysqli_fetch_array($result))  
                 {  
                 ?>  
                      <tr>  
                           <td><?php echo $row["order_id"]; ?></td>  
                           <td><?php echo $row["order_customer_name"]; ?></td>  
                           <td><?php echo $row["order_item"]; ?></td>  
                           <td>$ <?php echo $row["order_value"]; ?></td>  
                           <td><?php echo $row["order_date"]; ?></td>  
                      </tr>  
                 <?php  
                 }  
                 ?>  
                 </table>  
            </div>  
       </div>  
  </body>  
  </html>  
  <script>  
  $(document).ready(function(){  
       $.datepicker.setDefaults({  
            dateFormat: 'yy-mm-dd'   
       });  
       $(function(){  
            $("#from_date").datepicker();  
            $("#to_date").datepicker();  
       });  
       $('#filter').click(function(){  
            var from_date = $('#from_date').val();  
            var to_date = $('#to_date').val();  
            if(from_date != '' && to_date != '')  
            {  
                 $.ajax({  
                      url:"print.php",  
                      method:"POST",  
                      data:{from_date:from_date, to_date:to_date},  
                      success:function(data)  
                      {  
                           $('#order_table').html(data);  
                      }  
                 });  
            }  
            else  
            {  
                 alert("Please Select Date");  
            }  
       });  
  });  
  </script>

我正在尝试使用此处的库将流转换为Blob https://www.npmjs.com/package/stream-to-blob。但是,测试失败。如果我尝试 test("should submit front document", async () => { const side = "front"; const stream = fs.createReadStream(process.cwd() + "/test/resources/" + "id/front.jpg"); const image = await streamToBlob(stream); const front = await myLibrary.document(id, side, image); expect(front.success).toBe(true); }); ,则会得到以下console.log(image)

为什么blob为空{}?

Blob {}

0 个答案:

没有答案