JQ331
23 سبتمبر 2021، 1:36م
1
أنا أحاول معرفة كيفية السماح برفع الملفات عبر واجهة برمجة التطبيقات. لقد رأيت هذا المنشور المفيد: https://meta.discourse.org/t/how-to-upload-a-file-via-api-and-get-its-url/123732/3،
لكنه لا يوضح الصيغة الفعلية—وهذا ما أواجه صعوبة فيه. هل لدى أحدكم الصيغة الفعلية لاستدعاء ajax؟
هذا ما لدي:
method: "post",
url: [site]/uploads.json,
contentType: "multipart/form-data",
data: {
type: "composer",
file: '/path_to_file_from_users_machine'
},
headers: {
"User-Api-Key": key
},
dataType: "json",
}).then((result)=>{
لكن هذا يعطي استجابة 422—هناك شيء غير صحيح تمامًا في هذه الصيغة. كيف يمكنني جعلها تعمل؟
إعجاب واحد (1)
RGJ
(Richard - Communiteq)
27 سبتمبر 2021، 3:12م
2
JQ331:
استجابة 422
هل توجد رسالة ترافق هذه الاستجابة؟ يجب أن تمنحك ذلك مزيدًا من المعلومات.
JQ331
27 سبتمبر 2021، 4:01م
3
بحسب ما أستطيع رؤيته، هذا هو الرد الوحيد:
Error: Request failed with status code 422
ولا أرى أي شيء في السجلات.
blake
(Blake Erickson)
27 سبتمبر 2021، 8:38م
4
إليك مثال على Node نجح مع شخص ما وقد يساعدك:
I got this to work, like this:
import Axios from "axios";
import FormData from "form-data";
import fs from "fs";
const http = Axios.create({
baseURL: "https://forum.zeebe.io",
headers: {
"Api-Key":
"...",
"Api-Username": "...",
"Content-Type": "application/json",
Accept: "application/json",
},
});
http.interceptors.request.use((config) => {
if (config.data instanceof FormData) {
Object.assign(config.headers, config.data.getHeaders());
}
return config;
});
…
JQ331
27 سبتمبر 2021، 9:53م
5
شكرًا جزيلاً، @blake . هذا منشور مفيد لم أكن قد رأيته من قبل (رغم بحثي عن منشور مثله). سيساعدني بالتأكيد على المضي قدمًا. مع ذلك، لفت انتباهي شيء واحد. هذا السطر في الكود المرتبط:
const file = fs.readFileSync(filename);
هذا السطر يوحي بأن ما ترفعه إلى Discourse ليس الملف نفسه فعليًا، بل محتويات الملف (في الكود أعلاه، المتغير file يساوي السلسلة النصية المرتجعة من قراءة الملف). هذا مفاجئ بالنسبة لي، وأتساءل ما الذي يعنيه ذلك عند رفع الملفات الثنائية (أو ملفات الوسائط — التي تُرفع بنجاح إلى Discourse مباشرةً إذا كان الحجم مناسبًا، لكنني لست متأكدًا من كيفية عمل قراءتها أولًا).
ولكن سأتحقق من الأمر.
إعجاب واحد (1)
مرحباً بالجميع، بعد بعض الصعوبات والتجربة والخطأ، فكرت في مشاركة عينة PHP تعمل لتحميل الملفات إلى Discourse! إليكم. كما أنني أقوم بإنشاء مكتبة محدثة في الأصل من communiteq/discourse-api-php: عميل واجهة برمجة تطبيقات PHP لـ Discourse (github.com) :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/uploads.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array(
'type' => 'composer',
'synchronous' => 'true',
'file'=> new CURLFILE('/directory/filename.txt')),
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data',
'Accept: application/json',
'Api-Key: <your api key>',
'Api-Username: <your api user id>'
),
));
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r(json_decode($response,1));
print_r($info);
?>
عينة الإخراج:
Array
(
[id] => 627
[url] => https://example.com/uploads/default/original/1X/4661e3a873857e04440bcc3e4290e241eb6c08d7.txt
[original_filename] => filename.txt
[filesize] => 461
[width] =>
[height] =>
[thumbnail_width] =>
[thumbnail_height] =>
[extension] => txt
[short_url] => upload://a2D9hHsjwsdrVWkzlu5Go4c1VvV.txt
[short_path] => /uploads/short-url/a2D9hHsjwsdrVWkzlu5Go4c1VvV.txt
[retain_hours] =>
[human_filesize] => 461 Bytes
[dominant_color] =>
[thumbnail] =>
)
Array
(
[url] => https://example.com/uploads.json
[content_type] => application/json; charset=utf-8
[http_code] => 200
[header_size] => 422
[request_size] => 1244
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.029617
[namelookup_time] => 0.00046
[connect_time] => 0.003265
[pretransfer_time] => 0.003592
[size_upload] => 911
[size_download] => 460
[speed_download] => 15531
[speed_upload] => 30759
[download_content_length] => 460
[upload_content_length] => 911
[starttransfer_time] => 0.029604
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 192.168.192.40
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.192.18
[local_port] => 61613
[http_version] => 2
[protocol] => 1
[ssl_verifyresult] => 0
[scheme] => http
[appconnect_time_us] => 0
[connect_time_us] => 3265
[namelookup_time_us] => 460
[pretransfer_time_us] => 3592
[redirect_time_us] => 0
[starttransfer_time_us] => 29604
[total_time_us] => 29617
[effective_method] => POST
[capath] =>
[cainfo] =>
)
أيضًا، لقد حصلت على مساعدة كبيرة مع الأمثلة من عينات Postman هنا: Creates an upload | Discourse | Postman API Network
تحياتي
5 إعجابات