質問編集履歴

4

変更

2022/08/09 20:07

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,101 +1 @@
1
1
  node.jsでmulterでreq.fileがunderfinedになります。
2
- 画像を連続でPOST送信すると、そのうち何回かで起こります。
3
- C++でおこるもので、同様に実装したpythonでは起こりません。
4
-
5
- 送信されたPOSTをWiresharkで解析したところ、失敗する場合は、
6
- データ末尾にlast boudaryではなく、ただのboundaryと表記されており、実際にはデータはないのに、続きのデータがあるような表示になっていました。
7
-
8
- ```javascript
9
- const express = require('express')
10
- const app = express()
11
- const multer = require('multer')
12
- app.use(express.json())
13
-
14
- app.listen(3000, () => console.log('Example app listening on port 3000!'))
15
-
16
- app.post('/', multer({ dest: 'temp/' }).single('file'),
17
- async (req, res, next) => {
18
-
19
- if(typeof req.file == "undefined")
20
- {
21
- console.log("ファイルデータなし")
22
- return res.status(400).send('POST request')
23
- }
24
-
25
- console.log("ファイルデータあり")
26
- console.log( "File : " + req.file.path)
27
- res.status(200).send('POST request')
28
- })
29
- ```
30
-
31
- C++で画像をPOST
32
- ```C++
33
- #include "cpprest/http_client.h"
34
- #include "cpprest/filestream.h"
35
-
36
- pplx::task<void> SendImageTest(const std::string& _image_org_path, const std::string& _image_org_name, int _index)
37
- {
38
- return pplx::create_task([=]
39
- {
40
- cv::Mat img = cv::imread(_image_org_path);
41
- std::vector<unsigned char> buff;
42
- cv::imencode(".png", img, buff);
43
- std::string file_content(buff.begin(), buff.end());
44
-
45
- // Body作成
46
- std::string boundary = GetBoundary();
47
- std::string body = "\r\n--" + boundary
48
- + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + _image_org_name + "\""
49
- + "\r\nContent-Type: text/plain"
50
- + "\r\n\r\n" + file_content
51
- + "\r\n--" + boundary+ "--\r\n";
52
-
53
- // リクエスト作成
54
- http_request request;
55
- request.set_method(methods::POST);
56
- request.headers().add(L"Content-Type", L"multipart/form-data; boundary=" + convString(boundary));
57
- request.headers().add(L"accept", L"*/*");
58
- request.headers().add(L"accept-encoding", L"gzip, deflate");
59
- request.set_body(body);
60
-
61
- // リクエスト
62
- http_client_config config;
63
- config.set_validate_certificates(false);
64
- std::wstring url;
65
- url = L"http://localhost:3000";
66
-
67
- http_client client(url.c_str());
68
- return client.request(request);
69
- })
70
-
71
- .then([](http_response response)
72
- {
73
- if (response.status_code() == status_codes::OK)
74
- {
75
- }
76
- });
77
- }
78
-
79
- int main()
80
- {
81
- int image_count = 3;
82
- std::string save_image_path = "C:\\image";
83
-
84
- for(int i = 0; i < image_count; ++i)
85
- {
86
- std::string image_org_name = format("image%d.png", i);
87
- std::string image_org_path = save_image_path + "\\" + image_org_name;
88
- SendImageTest(image_org_path, image_org_name, image_i).wait();
89
- }
90
- return 0;
91
- }
92
- ```
93
-
94
- Pythonで画像をPOST
95
- ```Python
96
- for i in range(5):
97
- image_org_name = f"image-{i}"
98
- path = os.path.join("C:\\image", image_org_name + ".png")
99
- files = {'file': open(path, 'rb')}
100
- res = requests.post("http://localhost:3000", files=files, verify=False)
101
- ```

3

追記

2022/08/09 10:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,9 @@
1
1
  node.jsでmulterでreq.fileがunderfinedになります。
2
2
  画像を連続でPOST送信すると、そのうち何回かで起こります。
3
3
  C++でおこるもので、同様に実装したpythonでは起こりません。
4
+
5
+ 送信されたPOSTをWiresharkで解析したところ、失敗する場合は、
6
+ データ末尾にlast boudaryではなく、ただのboundaryと表記されており、実際にはデータはないのに、続きのデータがあるような表示になっていました。
4
7
 
5
8
  ```javascript
6
9
  const express = require('express')

2

修正

2022/08/09 09:54

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,5 @@
1
- node.jsでmulterでreq.fileがunderfinedにな
1
+ node.jsでmulterでreq.fileがunderfinedになります
2
- 画像を連続で送ると、そのうち何回かで起こります。
2
+ 画像を連続でPOST信すると、そのうち何回かで起こります。
3
3
  C++でおこるもので、同様に実装したpythonでは起こりません。
4
4
 
5
5
  ```javascript
@@ -69,11 +69,6 @@
69
69
  {
70
70
  if (response.status_code() == status_codes::OK)
71
71
  {
72
- printf("成功\n\n");
73
- }
74
- else
75
- {
76
- printf("失敗\n\n");
77
72
  }
78
73
  });
79
74
  }

1

syuusei

2022/08/09 09:50

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- node.jsでmulterでfileがunderfinedになる。
1
+ node.jsでmulterでreq.fileがunderfinedになる。
test CHANGED
@@ -1,7 +1,103 @@
1
- node.jsでmulterでfileがunderfinedになる。
1
+ node.jsでmulterでreq.fileがunderfinedになる。
2
2
  画像を連続で送ると、そのうち何回かで起こります。
3
3
  C++でおこるもので、同様に実装したpythonでは起こりません。
4
4
 
5
5
  ```javascript
6
+ const express = require('express')
7
+ const app = express()
8
+ const multer = require('multer')
9
+ app.use(express.json())
10
+
11
+ app.listen(3000, () => console.log('Example app listening on port 3000!'))
12
+
13
+ app.post('/', multer({ dest: 'temp/' }).single('file'),
14
+ async (req, res, next) => {
15
+
16
+ if(typeof req.file == "undefined")
17
+ {
18
+ console.log("ファイルデータなし")
19
+ return res.status(400).send('POST request')
20
+ }
21
+
22
+ console.log("ファイルデータあり")
23
+ console.log( "File : " + req.file.path)
24
+ res.status(200).send('POST request')
6
- コード
25
+ })
7
26
  ```
27
+
28
+ C++で画像をPOST
29
+ ```C++
30
+ #include "cpprest/http_client.h"
31
+ #include "cpprest/filestream.h"
32
+
33
+ pplx::task<void> SendImageTest(const std::string& _image_org_path, const std::string& _image_org_name, int _index)
34
+ {
35
+ return pplx::create_task([=]
36
+ {
37
+ cv::Mat img = cv::imread(_image_org_path);
38
+ std::vector<unsigned char> buff;
39
+ cv::imencode(".png", img, buff);
40
+ std::string file_content(buff.begin(), buff.end());
41
+
42
+ // Body作成
43
+ std::string boundary = GetBoundary();
44
+ std::string body = "\r\n--" + boundary
45
+ + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + _image_org_name + "\""
46
+ + "\r\nContent-Type: text/plain"
47
+ + "\r\n\r\n" + file_content
48
+ + "\r\n--" + boundary+ "--\r\n";
49
+
50
+ // リクエスト作成
51
+ http_request request;
52
+ request.set_method(methods::POST);
53
+ request.headers().add(L"Content-Type", L"multipart/form-data; boundary=" + convString(boundary));
54
+ request.headers().add(L"accept", L"*/*");
55
+ request.headers().add(L"accept-encoding", L"gzip, deflate");
56
+ request.set_body(body);
57
+
58
+ // リクエスト
59
+ http_client_config config;
60
+ config.set_validate_certificates(false);
61
+ std::wstring url;
62
+ url = L"http://localhost:3000";
63
+
64
+ http_client client(url.c_str());
65
+ return client.request(request);
66
+ })
67
+
68
+ .then([](http_response response)
69
+ {
70
+ if (response.status_code() == status_codes::OK)
71
+ {
72
+ printf("成功\n\n");
73
+ }
74
+ else
75
+ {
76
+ printf("失敗\n\n");
77
+ }
78
+ });
79
+ }
80
+
81
+ int main()
82
+ {
83
+ int image_count = 3;
84
+ std::string save_image_path = "C:\\image";
85
+
86
+ for(int i = 0; i < image_count; ++i)
87
+ {
88
+ std::string image_org_name = format("image%d.png", i);
89
+ std::string image_org_path = save_image_path + "\\" + image_org_name;
90
+ SendImageTest(image_org_path, image_org_name, image_i).wait();
91
+ }
92
+ return 0;
93
+ }
94
+ ```
95
+
96
+ Pythonで画像をPOST
97
+ ```Python
98
+ for i in range(5):
99
+ image_org_name = f"image-{i}"
100
+ path = os.path.join("C:\\image", image_org_name + ".png")
101
+ files = {'file': open(path, 'rb')}
102
+ res = requests.post("http://localhost:3000", files=files, verify=False)
103
+ ```