第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從 NodeJS 上傳多張圖片到 PHP 服務(wù)器

從 NodeJS 上傳多張圖片到 PHP 服務(wù)器

寶慕林4294392 2023-05-11 13:59:49
NodeJS 開(kāi)發(fā)人員,我有一種情況,我必須將本地圖像從 NodeJS 上傳到 PHP 服務(wù)器。沒(méi)有報(bào)錯(cuò),請(qǐng)求成功 200 OK,但是圖片沒(méi)有出現(xiàn)在PHP服務(wù)器中。注意:我是一名 nodeJS 開(kāi)發(fā)人員,在 PHP 方面沒(méi)有太多經(jīng)驗(yàn)。這是想做的事情:在 NodeJs 中,有一個(gè)文件列表,現(xiàn)在我想將這些文件一個(gè)一個(gè)地附加到 formData 中,之后,我必須將這個(gè)表單數(shù)據(jù)發(fā)布到 PHP 服務(wù)器,以便將圖片上傳到 PHP 服務(wù)器,在 PHP 服務(wù)器中,在第一行,它從 post 請(qǐng)求接收文件,然后循環(huán)將具有支持?jǐn)U展名的文件一個(gè)一個(gè)地移動(dòng)到上傳目錄,循環(huán)完成后它發(fā)回一個(gè)包含文件數(shù)組的響應(yīng)'下載地址一切正常,我已經(jīng)安慰了一切,一切都有完美的價(jià)值,甚至 HTTPS POST 請(qǐng)求也成功了,但是 PHP 服務(wù)器沒(méi)有圖像,PHP 服務(wù)器沒(méi)有接收請(qǐng)求中的文件,所以看起來(lái)多部分?jǐn)?shù)據(jù)不起作用NodeJs??雌饋?lái) formData 在 NodeJs 中不能正常工作,因?yàn)橄嗤拇a在瀏覽器中的客戶端 JS 上工作。節(jié)點(diǎn):- const Axios = require('axios'); const Fs = require('fs'); const FormData = require('form-data'); var formData = new FormData(); //here allFiles is an array of object , each object contains 1 file with details like local file path ,name,sizefor (var index = 0; index < allFiles.length; index++) { //Append multiple files to formData.            let currentFile = allFiles[index]            //createReadStream Retyrns fileData            let fileWithInfo = await Fs.createReadStream(currentFile.uri)            formData.append("files[]", fileWithInfo );       } Axios.post('example.com/upload.php',formData,     {         headers: formData.getHeaders()     })     .then(d => console.log("DONE=>>>> ", d.data))     .catch((f) => console.log('FAILED=>> ', f))    });PHP 代碼:-<?php// Count total files$countfiles = count($_FILES['files']['name']);// Upload directory$upload_location = "uploads/";// To store uploaded files path$files_arr = array();// Loop all filesfor($index = 0;$index < $countfiles;$index++){   // File name   $filename = $_FILES['files']['name'][$index];   // Get extension   $ext = pathinfo($filename, PATHINFO_EXTENSION);   // Valid image extension   $valid_ext = array("png","jpeg","jpg");   // Check extension   if(in_array($ext, $valid_ext)){     // File path     $path = $upload_location.$filename;     // Upload file     if(move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){        $files_arr[] = $path;     }   }}echo json_encode($files_arr);die;
查看完整描述

5 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

<?php

// Count total files

var_dump($_FILES['files']);


$countfiles = count($_FILES['files']['name']);

echo $countfiles;


// Upload directory

$upload_location = "uploads/";


// To store uploaded files path

$files_arr = array();


// Loop all files

for($index = 0;$index < $countfiles;$index++){

  echo $index;


   // File name

  $filename = $_FILES['files']['name'][$index];

  echo $filename;


   // Get extension

  $ext = pathinfo($filename, PATHINFO_EXTENSION);

  echo $ext;


   // Valid image extension

   $valid_ext = array("png","jpeg","jpg", "csv");


   // Check extension

   if(in_array($ext, $valid_ext)){


     // File path

     $path = $upload_location.$filename;


     // Upload file

     if(move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){

        $files_arr[] = $path;

     }

   }


}


/* echo json_encode($files_arr); */

die;

const Axios = require('axios');

 const Fs = require('fs');

 const FormData = require('form-data');


const allFiles = [

  {uri: 'my/Spark_Dataset/combin.csv'},

  {uri: 'my/Spark_Dataset/combine-csv.csv'},

  // {uri: 'C:\\Users\\my\\combine-csv.csv'}

];



(async function() {

  var formData = new FormData();


  //here allFiles is an array of object , each object contains 1 file with details like local file path ,name,size

  for (var index = 0; index < allFiles.length; index++) { //Append multiple files to formData.

    // console.log(index);


    let currentFile = allFiles[index]

    //createReadStream Retyrns fileData

    // console.log(currentFile);

    let fileWithInfo = await Fs.createReadStream(currentFile.uri)

    formData.append("files[]", fileWithInfo );

  }


  console.log(formData);


  Axios.post('http://localhost:8000/upload.php',

    formData,

    {

      headers: formData.getHeaders(),

      // 'Content-Type':'multipart/form-data',

    })

    .then(d => console.log("DONE=>>>> ", d.data))

    .catch((f) => console.log('FAILED=>> ', f))

})();

packages.json


"axios": "^0.20.0",

"form-data": "^3.0.0",

"fs": "0.0.1-security",

首先我啟動(dòng)了本地服務(wù)器php -S localhost:8000


運(yùn)行本地服務(wù)器后,我運(yùn)行 index.js 文件,node index.js

我在本地測(cè)試了代碼,它工作正常。


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

Axios.post('example.com/upload.php', formData, {

  headers: formData.getHeaders()

})

.then(d => console.log("DONE=>>>> ", d.data))

.catch((f) => console.log('FAILED=>> ', f));

您缺少axios post 請(qǐng)求中您的情況,您可以直接傳遞 formData 或調(diào)用 formData.getBuffer() data。formData


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

https://github.com/form-data/form-data#buffer-getbuffer


const { readFile } = require('fs/promises');


for (let index = 0; index < allFiles.length; index++) {

  const currentFile = allFiles[index];

  const fileWithInfo = await readFile(currentFile.uri);

  formData.append('files[]', fileWithInfo);

}


axios.post('https://example.com/', formData.getBuffer(), formData.getHeaders());


// you have to send files as buffer


還有流 https://stackoverflow.com/a/53126159/8784402的解決方案


const response = await axios({

        method: 'post',

        url: 'http://www.yourserver.com/upload',

        data: formData,

        headers: {

        'content-type': `multipart/form-data; boundary=${form._boundary}`,

        },

    });


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

也許我的回答沒(méi)有解決,但我建議對(duì) PHP 代碼進(jìn)行一些更改。當(dāng)然,$_FILES 數(shù)組中的鍵不能是數(shù)字,最好使用 foreach 而不是 for。我允許自己稍微更改您的代碼以使其更小


<?php

// Upload directory

$upload_location = "uploads/";


// To store uploaded files path

$files_arr = array();


// Valid image extension

$valid_ext = array("png","jpeg","jpg");


foreach($_FILES['files']['name'] as $key => $file) {

    if(

        in_array(pathinfo($_FILES['files']['name'][$key], PATHINFO_EXTENSION), $valid_ext)

        &&

        move_uploaded_file($_FILES['files']['tmp_name'][$key],$upload_location.$_FILES['files']['name'][$key])

    ) {

        $files_arr[] = $upload_location.$_FILES['files']['name'][$key];

    } else {

        //Only for debugging

        $files_arr[] = 'Not uploaded: '.$upload_location.$_FILES['files']['name'][$key];

    }

}


echo json_encode($files_arr);

die;


查看完整回答
反對(duì) 回復(fù) 2023-05-11
?
慕桂英3389331

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

allFiles.length在 NodeJS 代碼中錯(cuò)過(guò)了循環(huán)中的參數(shù)。正確的代碼將是

for (var index = 0; index < allFiles.length; index++) {

PHP代碼是正確的,但是如果你發(fā)送一個(gè)空的files數(shù)組,它不會(huì)出錯(cuò)并且會(huì)返回成功響應(yīng)!


查看完整回答
反對(duì) 回復(fù) 2023-05-11
  • 5 回答
  • 0 關(guān)注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)