Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

antd upload上传图片到服务器前端怎么配置啊

需要实现图片直穿到七牛云,我从后台接口拿token,然后传到七牛,返回文件名,我渲染到界面,想问下,antd upload组件怎么配置,可以实现此功能

目前进度:
用upload怎么上传七牛时携带其他参数,现已将token等参数可以添加至data(file)中了,但是上传是结构不对
------WebKitFormBoundaryHnlWKpB0F38d8Wva
Content-Disposition: form-data; name="file"; filename="尼古拉斯赵四.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryHnlWKpB0F38d8Wva--

应该是这样的结构
------WebKitFormBoundaryh46aOmMD0U611gS6
Content-Disposition: form-data; name="file"; filename="尼古拉斯赵四.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryh46aOmMD0U611gS6
Content-Disposition: form-data; name="key"

c8dcb68SSSSSSSSSSSSSSSSc9f1864
------WebKitFormBoundaryh46aOmMD0U611gS6
Content-Disposition: form-data; name="token"

KT9eQLuXXXXXXXXXXXXXXXTQ4NDcxODc5NX0=
------WebKitFormBoundaryh46aOmMD0U611gS6--

data: function(file,token,key){
    console.log(file);
    var values = {"bucket":"article"};
    $.ajax({
        url:'/api/2.1/xxxxx/get-xxxx-token',
        type: 'GET',
        dataType: 'json',
        data: values,
        success: function(map){
            var uptoken = map.data.token;
            var name = map.data.filename;
            token = uptoken;
            key = name;
            file.token = uptoken;
            file.key = name;
        }
    });
    console.log(file);
},

以下是本人的解决方案,前端的,需要后端提供token接口。

 const props = {
      action: 'http://upload.qiniu.com',
      listType: 'picture',
      data: function(){
          //请求token需要带的参数,后端提供
          var values = {
              "bucket":"xxxx",
              session_id: xxx
          };
          var token;
          var key;
          $.ajax({
            url:'/api/2.1/oaaa/get-token',
            type: 'GET',
            dataType: 'json',
            data: values,
            async:false,
            success: function(map){
              token = map.data.token;
              key = map.data.filename;
            }
          });
          return{
              key: key,
              token: token
          }
      },
      onChange(info){
          // 这个上传成功后前端界面显示的图
          var qiniuDomain = "http://oaaaaaaaaa.com/";
          if (info.file.status === 'done') {
          info.file.thumbUrl = qiniuDomain + info.file.response.key;
          info.file.url = qiniuDomain + info.file.response.key;
          $("#thumb").val(info.file.url);
        }
      }
    }


组件:

<Upload {...props}>
    <Button type="ghost">
        <Icon type="upload" /> 上传
    </Button>
    <Input id="thumb" />
    <Input id="content" />
</Upload>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

data应该是一个参数对象或者是返回参数对象的方法

data: function() {
    // code
    return {
        key: name,
        token: token
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...