c# - 在 XAMARIN C# 中使用 FILE 发送 POST 请求
问题描述
在网上看,我看到了一点如何发布“文件”,因此,我以这种方式编写了我的代码:
var upfilebytes = File.ReadAllBytes((string)fPath);
//create new HttpClient and MultipartFormDataContent and add our file, and StudentId
HttpClient client = new HttpClient();
MultipartFormDataContent content = new MultipartFormDataContent();
ByteArrayContent baContent = new ByteArrayContent(upfilebytes);
content.Add(baContent, "img", fPath);
StringContent emailText = new StringContent(lbl_email.Text);
content.Add(emailText, "email");
string url = "http://192.168.178.77/TestLoginURL/api/updateUserImage.php";
//upload MultipartFormDataContent content async and store response in response var
var response =
await client.PostAsync(url, content);
//read response result as a string async into json var
var responsestr = response.Content.ReadAsStringAsync().Result;
现在问题是另一个......这是我的 API 代码:
<?php
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
//getting values
$img = $_FILES['img']['name'];
$email = $_POST['email'];
//including the db operation file
require_once '../includes/DbOperation.php';
$db = new DbOperation();
$target = "/Applications/XAMPP/xamppfiles/htdocs/TestLoginURL/images/".basename($img);
//inserting values
if($db->updateImage((String)basename($img),$email)){
$response['error']=false;
$response['message']='Image added successfully - test fileName = '.(String)basename($img);
}else{
$response['error']=true;
$response['message']='Could not add image';
}
if(move_uploaded_file($_FILES['img']['tmp_name'], $target)) {
/*$response['error']=false;
$response = "Image uploaded successfully";*/
}else{
$response['error']=true;
$response = "Failed to upload image";
}
}else{
$response['error']=true;
$response['message']='You are not authorized';
}
echo json_encode($response);
使用 Postman 它可以完美运行,更新数据库中图像的名称并将图像物理插入指定路径,响应消息例如是这样的:{“error”:false,“message”:“图像添加成功 - 测试文件名 = trollolollo.png"}
现在,应用程序将文件保存在正确的存储库中,但不会更新数据库中“图像”的名称……但奇怪的是,调试器中的“响应”消息也正确显示了文件的名称……所以我只是不明白我错在哪里......有人可以帮我写代码吗?谢谢
OFF_TOPIC:通常,当我只需要发送字符串时,我会发送一个以这种方式编写的 post 请求
string url = "http://192.168.178.77/TestLoginURL/api/insertUser.php";
FormUrlEncodedContent formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("nickname", nickname),
new KeyValuePair<string, string>("password", password1),
new KeyValuePair<string, string>("email", email)
});
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
try
{
CancellationTokenSource cts = new CancellationTokenSource();
var responseMessage = httpClient.PostAsync(url, formContent).Result;
}
catch (Exception ex)
{
ex.Message.ToString();
throw;
}
但是现在,作为一个文件并且没有经验,我遇到了困难...再次感谢,我希望有人可以帮助我编写代码
解决方案
您确定您正在正确处理更新查询吗?
如果你搞砸了 SQL 查询或类似的东西,更新查询只会在失败时返回 false。因此,您必须使用mysqli_stmt_affected_rows来查看您的 PHP 代码中是否更新了一行。
如果邮递员可以做到,HttpClient 也必须能够做到,只要配置正确。
尝试使用邮递员正在使用的所有标题,您可能遗漏了一些东西,也许是文件名导致数据库查询失败。
顺便说一句,您在服务器中处理 jpg 和 png 的方式有什么区别吗?你也可以检查一下。
推荐阅读
- python - 使用带有 RS232 端口的 pymodbus 从 CLICK PLC 读取计数器值到 python
- java - Maven项目中的构建顺序
- c++ - 关于 C++ 中星号和字符串连接的问题
- java - 弹簧未初始化 - 应用程序未启动
- javascript - Javascript Do while 循环仅在一次迭代后停止
- excel - 获取从另一个页面复制的表格名称
- awk - 如何在忽略较少数量的连续行的同时将每块 3 行合并在一起?
- css - 换行 Office UI Fabric DetailsRow 的文本
- javascript - 反应通过日期作为道具:“未定义不可迭代”
- ros - 如何从 MD49 编码器输出发布 odom (nav_msgs/Odometry)?