nuiapp
1、电脑端调试可以正常上传图片,但是在手机上调试或是安装在手机上都不能上传图片,
2、已经勾选相机

3、nuiapp的上传代码
// 调用拍照功能
takePhoto() {
// 调用uniapp API选择或拍照
uni.chooseImage({
count: 1, // 默认9, 设置图片的数量
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机,默认二者都有
success: (res) => {
// 成功选择图片后的回调
const tempFilePaths = res.tempFilePaths;
// 使用uploadFile上传图片
this.uploadPhoto(tempFilePaths[0]);
uni.showToast({
title: '图片传上成功了',
icon: 'none'
});
},
fail: (err) => {
// 处理错误情况
console.log('chooseImage fail', err);
uni.showToast({
title: '上传失败',
icon: 'none'
});
}
});
// 上传图片到服务器
uploadPhoto(photoPath) {
// 显示加载中的提示
uni.showLoading({
title: '上传中...'
});
// 使用uniapp API上传文件
uni.uploadFile({
url: 'http://localhost:21371/api/Images/upload', // 服务器上传接口
filePath: photoPath,
name: 'file', // 必须填写,为了后端接收文件流的参数名字
formData: {
// 其他要上传的参数
imageId:'123'
},
success: (uploadFileRes) => {
// 上传成功的回调
console.log('uploadFile success:', uploadFileRes);
uni.hideLoading();
},
fail: (uploadFileErr) => {
// 上传失败的回调
console.log('uploadFile fail:', uploadFileErr);
uni.hideLoading();
}
});
},
4、后端上传代码
[HttpPost("upload")]
// public IActionResult UploadImage(IFormFile file)
public string UploadImage([FromForm] LongModel.ITEMS model, IFormFile file)
{
// 保存图片到数据库
byte[] imageData = null;
using (var memoryStream = new MemoryStream())
{
file.CopyTo(memoryStream);
imageData = memoryStream.ToArray();
}
// 插入图片数据到数据库
using (var connection = new SqlConnection(sqlcon))
{
connection.Open();
using (var command = new SqlCommand($"INSERT INTO Images (ImageData,imageId) VALUES (@ImageData,'{model.imageId}')", connection))
{
command.Parameters.AddWithValue("@ImageData", imageData);
//command.Parameters.AddWithValue("@imageId", '1');
command.ExecuteNonQuery();
}
}
return Utils.ResponseWrite("y", "yes", "数据处理成功"); //Ok("图片上传成功");//
}
5、后端查询代码,请提供一个uniapp 查询图片代码
[HttpGet("getImage/{imageId}")]
public IActionResult GetImage(int imageId)
{
// 从数据库查询图片数据
byte[] imageData = null;
using (var connection = new SqlConnection(sqlcon))
{
connection.Open();
using (var command = new SqlCommand("SELECT ImageData FROM Images WHERE ImageId = @ImageId", connection))
{
command.Parameters.AddWithValue("@ImageId", imageId);
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
imageData = (byte[])reader["ImageData"];
}
}
}
}
if (imageData == null)
{
return NotFound();
}
// 将图片数据转换为图片并返回
return File(imageData, "image/jpeg");
}