<uni-section title="图片上传" type="line">
<view class="example">
<view class="example-body">
<uni-file-picker limit="9" v-model="imageValue" mode="grid" file-mediatype="image" :auto-upload="false" @select="selectImage">
</uni-file-picker>
</view>
</view>
</uni-section>
methods: {
async selectImage(e) {
var that = this;
// console.log("选择图片:" + e.tempFilePaths);
// 循环所有选择的图片
for (let i = 0; i < e.tempFilePaths.length; i++) {
console.log("选择图片:" + e.tempFilePaths.length);
await that.uploadImg(e.tempFilePaths[i]);
}
},
async uploadImg(tempFilePath) {
var that = this;
if (!tempFilePath) return;
that.imageValue.push({
url: tempFilePath,
name: ""
});
const [err, {
data
}] = await uni.uploadFile({
url: app.globalData.rqurl + '/InvoicingWeb/InvoicingController.aspx?method=getPictures',
filePath: tempFilePath,
name: 'file',
formData: {
'file_name': that.imageValue.length + ".jpg"
},
header: {
"Content-Type": "multipart/form-data",
"Content-Disposition": "form-data"
}
});
console.log("err", err)
console.log("data:" + data);
if (!that.isGuid(data)) {
// upload fail
that.imageValue.pop()
uni.showToast({
title: "上传失败",
icon: "none"
})
} else {
// upload success
that.imageValue[that.imageValue.length - 1].name = data
}
},
isGuid(str) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(str);
},
public string getPictures()
{
Result rs = new Result();
var postfiles = Request.Files;
if (postfiles.Count <= 0)
{
rs.isok = false;
rs.remark = "请选择图片";
return JObject.FromObject(rs).ToString();
}
string path = Server.MapPath("~/uploadFile/image/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// 循环所有上传的文件
for (int i = 0; i < postfiles.Count; i++)
{
var postfile = postfiles[i];
if (postfile == null || postfile.ContentLength <= 0)
{
continue;
}
string fileName = Path.GetFileName(postfile.FileName);
string saveName = Guid.NewGuid().ToString() + Path.GetExtension(fileName);
string savePath = Path.Combine(path, saveName);
postfile.SaveAs(savePath);
}
rs.isok = true;
rs.obj = "上传成功";
return JObject.FromObject(rs).ToString();
}
这是目前的代码,目前是选择了多张图片,但是到后面保存的时候只保存了一张,比如选了三张,只保存了一张。
需求是,选择了图片上传成功后再回显图片,然后保存了表单之后,重新进入页面从数据库读取显示图片,是需要用到这个手动上传this.$refs.files.upload()这个方法吗,目前是一选择就直接显示了
在前端 uni-app 中使用 uni-file-picker 组件,选择多个文件,并存储到本地数组。
<template>
<view class="page">
<view class="img-list">
<view v-for="(item, index) in imgList" :key="index" class="img-item">
<image :src="item" class="img-content"></image>
<text class="del-icon" @click="delImg(index)">×</text>
</view>
</view>
<uni-file-picker :multiple="true" @onChange="uploadImg"></uni-file-picker>
</view>
</template>
JS部分代码
<script>
export default {
data() {
return {
imgList: []
};
},
methods: {
uploadImg(e) {
const tempFilePaths = e.detail.tempFilePaths;
this.imgList = [...this.imgList, ...tempFilePaths];
},
delImg(index) {
this.imgList.splice(index, 1);
}
}
};
</script>
C# 部分代码,在 C# .Net 后端中处理图片上传的请求,并存储到数据库或本地磁盘。
using System;
using System.IO;
using System.Web;
namespace ImageUploader
{
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string fileName = context.Server.MapPath("~/UploadedImages/" + file.FileName);
file.SaveAs(fileName);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("Image(s) Uploaded Successfully");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
它使用了一个名为"uni-app"的基于Vue.js的框架。代码使用uni-file-picker组件,允许用户选择最多9张图像。当图像选择时,将触发selectImage方法,该方法反过来调用uploadImg方法。该方法将每个选定的图像一个一个上传到服务器端的端点(/InvoicingWeb/InvoicingController.aspx?method=getPictures)。
后端代码是用C#编写的。它从前端接收作为postfile的上传图像,然后将图像数据写入文件流,以创建图像文件("~uploadFile/image/1.jpg")。该方法然后创建一个结果对象,其中rs.isok = true,并将上传的图像数据作为rs.obj,并以JSON字符串的形式返回结果。
前端代码是基于uni-app开发框架实现的。该代码中定义了一个selectImage方法,该方法调用uploadImg方法上传文件到后端。uploadImg方法中,使用uni.uploadFile API上传图片,并将返回的结果判断是否上传成功。
后端代码是C#代码,代码中定义了一个getPictures方法。该方法接收前端上传的图片,并将图片保存到服务器端。
可以尝试使用文件上传组件unp-picker,前端使用该组件选择图片后,调用uploadImg方法上传文件到后端。后端接收到请求后,使用FileStream类保存图片文件。
不知你解决了没?
如果没有解决,我们可以交流交流
前端拿到上传图片的数组后,需要遍历,没有遍历的话,后端接收到的图片会被覆盖
let files = []; //这个数组才是真正传到后端数据
this.tempFilePaths.forEach((url, index) => {
files.push({
uri: url,
name: `files[${index}]`
})
})