image.png
    之前写图片上传都是通过自定义方法将file和OSS信息组装到formData里面然后设置请求头’Content-Type’: ‘multipart/form-data’来上传的,现在直接用自带的action上传还是挺方便的。

    1. <template>
    2. <a-upload
    3. :action="urlhost"
    4. listType="picture-card"
    5. accept="image/*"
    6. @preview="handlePreview"
    7. @change="handleChange"
    8. :remove="removeImg"
    9. :beforeUpload="beforeUpload"
    10. :data="paramsData">
    11. <a-icon type="plus" />
    12. <div class="ant-upload-text">点击上传</div>
    13. </a-upload>
    14. <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
    15. <img alt="example" style="width: 100%" :src="previewImage" />
    16. </a-modal>
    17. </template>
    18. <script>
    19. function getBase64(file) {
    20. return new Promise((resolve, reject) => {
    21. const reader = new FileReader();
    22. reader.readAsDataURL(file);
    23. reader.onload = () => resolve(reader.result);
    24. reader.onerror = error => reject(error);
    25. });
    26. }
    27. import {getOssSignWeb} from '../../../api/upload.js'; //获取后端OSS信息接口
    28. export default {
    29. return {
    30. businessId:'',//业务id
    31. previewVisible:false,
    32. previewImage: '',
    33. paramsData:{ //OSS信息对象 也是用来需要上传的参数
    34. 'OSSAccessKeyId':'',
    35. 'policy':'',
    36. 'Signature':'',
    37. 'key':'',
    38. 'success_action_status':200
    39. },
    40. urlhost:'', //上传路径
    41. },
    42. methods: {
    43. async handlePreview(file) { //点击图片预览
    44. if (!file.url && !file.preview) {
    45. file.preview = await getBase64(file.originFileObj);
    46. }
    47. this.previewImage = file.url || file.preview;
    48. this.previewVisible = true;
    49. },
    50. beforeUpload(file, fileList){ //在上传前获取OSS信息
    51. return new Promise((resolve,reject)=>{
    52. let params ={
    53. "businessId":this.businessId,
    54. "suffix":'.jpg'
    55. }
    56. getOssSignWeb(params).then(res=>{
    57. this.urlhost = res.retData.host
    58. this.paramsData = { //组装参数
    59. 'OSSAccessKeyId':res.retData.accessid,
    60. 'policy':res.retData.policy,
    61. 'Signature':res.retData.signature,
    62. 'key':res.retData.dir+res.retData.fileName,
    63. 'success_action_status':200
    64. }
    65. resolve(file)
    66. }).catch(e=>{
    67. reject()
    68. })
    69. })
    70. },
    71. removeImg(){
    72. return new Promise((resolve,reject)=>{
    73. this.$confirm({
    74. title: '提示',
    75. content: '是否删除这张图片',
    76. okText: '确定',
    77. okType: 'danger',
    78. cancelText: '取消',
    79. onOk() {
    80. resolve(true)
    81. },
    82. onCancel() {
    83. resolve(false)
    84. },
    85. })
    86. })
    87. },
    88. handleChange(info) {
    89. if (info.file.status === 'done') {
    90. this.$message.success('上传成功!');
    91. }
    92. if (info.file.status === 'error') {
    93. this.$message.error('上传失败!');
    94. }
    95. },
    96. handleCancel() {
    97. this.previewVisible = false;
    98. },
    99. }
    100. }
    101. </script>