前端代码(vue框架):
- <template>
- <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
- <el-form-item label="课程名称" prop="name">
- <el-input v-model="ruleForm.name">el-input>
- el-form-item>
- <el-form-item label="课程描述" prop="desc">
- <el-input type="textarea" v-model="ruleForm.desc">el-input>
- el-form-item>
- <el-form-item label="课程图片">
- <el-upload
- class="avatar-uploader"
- action="#"
- ref="upload"
- :http-request="httprequest"
- :show-file-list="false"
- :auto-upload="false"
- :on-change="hanchange"
- >
- <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar">
- <i v-else class="el-icon-plus avatar-uploader-icon">i>
- el-upload>
- el-form-item>
- <el-form-item>
- <el-button type="primary" @click="submitForm('ruleForm')">立即创建el-button>
- <el-button @click="resetForm('ruleForm')">重置el-button>
- el-form-item>
- el-form>
- template>
- <script>
- import axios from "axios";
- export default {
- data() {
- return {
- ruleForm: {
- name: '',
- desc: '',
- imageUrl:"",
- },
- rules: {
- name: [
- { required: true, message: '请输入课程名称', trigger: 'blur' },
- { max: 20, message: '最多20个字符', trigger: 'blur' }
- ],
- desc: [
- { required: true, message: '请填写课程描述', trigger: 'blur' },
- {max:20,message:'描述最多20个字符',trigger: "blur"}
- ]
- }
- };
- },
- methods: {
- httprequest(param){
- //拷贝图片到表单数据之中
- this.ruleForm.file=param.file
- },
- submitForm(formName) {
- this.$refs[formName].validate((valid) => {
- if (valid) {
- this.$refs.upload.submit()
- let formdata=new FormData()
- formdata.append("name",this.ruleForm.name)
- formdata.append("desc",this.ruleForm.desc)
- formdata.append("file",this.ruleForm.file)
- let config={
- header:{
- "Content-Type": "multipart/form-data"
- }
- }
- console.log(formdata)
- axios.post("http://localhost:8181/image",formdata,config)
- console.log("submit")
- } else {
- console.log('error submit!!');
- return false;
- }
- });
- },
- resetForm(formName) {
- this.$refs[formName].resetFields();
- this.ruleForm.imageUrl=''
- },
- hanchange(file){
- //显示图片
- this.ruleForm.imageUrl= URL.createObjectURL(file.raw);
- }
-
- }
- }
- script>
- <style>
- .avatar-uploader .el-upload {
- border: 1px dashed #d9d9d9;
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- }
- .avatar-uploader .el-upload:hover {
- border-color: #409EFF;
- }
- .avatar-uploader-icon {
- font-size: 28px;
- color: #8c939d;
- width: 178px;
- height: 178px;
- line-height: 178px;
- text-align: center;
- }
- .avatar {
- width: 178px;
- height: 178px;
- display: block;
- }
- style>
后端接口:(跨域请求这里小编使用的添加@CrossOrigin()注解)
- @Controller
- @ResponseBody
- //跨越请求
- @CrossOrigin("http://localhost:7070")
- public class webcontrller {
-
- //普通查询
- @Autowired
- private userservice userservice;
- @GetMapping("/users")
- public List
GetAlluser(){ - List
users = userservice.selectList(null); - return users;
- }
-
- //简单的分页效果的查询
-
- @GetMapping("/pages")
- public PageInfo
pages(@RequestParam(defaultValue = "5") Integer pagesize, - @RequestParam(defaultValue ="1") Integer pageNum ){
- PageHelper.startPage(pageNum,pagesize);
- PageInfo
pageInfo=new PageInfo<>(userservice.selectList(null)); - return pageInfo;
- }
- //头像上传回显
- @PostMapping("/image")
- public String url(MultipartFile file,String name,String desc) throws IOException {
- //获取图片的原始名字
- String originalFilename = file.getOriginalFilename();
- //获取文件的后缀
- String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
- //设置uuid
- String uuid= UUID.randomUUID().toString();
- //设置新的图片名字
- String filename=uuid+substring;
- System.out.println(filename);
- //获取文件的路径
- String path= "D:/网站素材图片/";
- file.transferTo(new File(path,filename));
- System.out.println(name+desc);
- return path+filename;
- }
-
- }
执行结果: