【发布时间】:2021-07-26 15:23:22
【问题描述】:
我想通过 POST 或 PUT 请求将多个 MAT 文件发送到 Matlab 中的服务器,但在构建 RequestMessage 时遇到问题。谁能帮我完成这项任务?我是 Matlab 新手,如果有任何帮助,我将不胜感激。
我的代码产生以下错误消息:
使用 RequestMessage 时出错
预计 RequestLine 为以下类型之一:
matlab.net.http.RequestLine
它的类型是字符串。
这是我的代码:
import matlab.net.http.*
import matlab.net.http.field.*
import matlab.net.*
% Define train and test directories
train_dir = "./data/train";
test_dir = "./data/test";
% Get all train files in train_dir
train = dir(fullfile(train_dir, "*.mat"));
train_files = [];
for k = 1:length(train)
full_file = fullfile(train_dir, train(k).name);
train_files = [train_files; full_file];
end
% Get all test files in test_dir
test = dir(fullfile(test_dir, "*.mat"));
test_files = [];
for k = 1:length(test)
full_file = fullfile(test_dir, test(k).name);
test_files = [test_files; full_file];
end
% Create POST request
provider = MultipartFormProvider(...
"train_files", FileProvider(train_files),...
"test_files", FileProvider(test_files));
% Request message and response
request = RequestMessage("PUT", [], provider);
response = request.send("http://127.0.0.1:5000/api/data_upload");
我已经用一个html站点实现了文件传输,效果很好。希望这有助于描述我的问题。这里是html模板:
<title>Upload train and test files</title>
<form action="" method="POST" enctype="multipart/form-data">
<h2>Train files</h2>
<p><input type="file" name="train_files" multiple="" accept=".mat">
<h2>Test files</h2>
<p><input type="file" name="test_files" multiple="" accept=".mat">
<p><input type="submit" value="Upload train AND test">
</p>
</form>
【问题讨论】:
标签: html matlab http http-post httprequest