问题描述
我正在尝试将文件从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)
此代码执行正常,我可以在Web服务器代码中看到请求正常,如下所示:
[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获取数据时,我可以看到file参数很好 我怀疑此问题与.net核心模型绑定的工作方式有关,但不确定... 这里有关于如何在服务器上显示我的文件的任何建议吗?
解决方法
解决了我的问题-问题是在Python中,我使用实际文件名“ ./run1.csv”而不是文字字符串“ file”将文件分配给上传字典。 更新此内容解决了我的问题。
file_dict = {"file": postFile}
这就是我相信@nalnpir所说的。 我通过从邮递员那里以及从我的python代码到http://httpbin.org/post的发布中进行比较并比较了respinse来解决了这个问题