【发布时间】:2023-03-15 22:50:01
【问题描述】:
我正在尝试将文件从 Python 脚本发送到我的 .net 核心网络服务器。
在 Python 中,我使用 requests 库执行此操作,我的代码如下所示。
filePath = "run-1.csv"
with open(filePath, "rb") as postFile:
file_dict = {filePath: postFile}
response = requests.post(server_path + "/batchUpload/create", files=file_dict, verify=validate_sql)
print (response.text)
这段代码执行得很好,我可以在我的网络服务器代码中看到请求很好,如下所示:
[HttpPost]
[Microsoft.AspNetCore.Authorization.AllowAnonymous]
public string Create(IFormFile file) //Dictionary<string, IFormFile>
{
var ms = new MemoryStream();
file.CopyTo(ms);
var text = Encoding.ASCII.GetString(ms.ToArray());
Debug.Print(text);
return "s";
}
但是,file 参数始终返回 null。 另外,从 postMan 获取数据时,我可以看到文件参数很好 我怀疑这个问题与 .net 核心模型绑定的工作方式有关,但不确定...... 关于如何让我的文件显示在服务器上的任何建议?
【问题讨论】:
-
我认为您在这里的问题是,您首先从 python 寻址到应该是文件的文件,其次,您确定您只发送一个文件,并且这是正确的发送方式一个文件?
标签: python c# asp.net-mvc asp.net-core .net-core