【问题标题】:Upload image to GraphQL server将图像上传到 GraphQL 服务器
【发布时间】:2017-06-22 20:30:41
【问题描述】:

我正在使用 react-dropzonegraphql-server-express-upload 在 GraphQL Apollo 中上传图片。在我的客户端中,文件如下所示:

但在服务器中,当我登录时,它看起来像这样:

{ preview: 'blob:http://localhost:3000/c622b0bd-3f0d-4f52-91f4-7676c8534c59' }

我正在按照graphql-server-express-upload 中的自述文件中的说明进行操作,但到目前为止还没有运气。如何获得完整的图像?

【问题讨论】:

  • 你为什么不使用静态休息路由上传,并捕获该休息路由的 responsefilepath 并使用文件路径的响应在数据库中更新并从路径中获取图像,而用户查询

标签: javascript node.js reactjs graphql react-apollo


【解决方案1】:

另一种方法是在客户端将二进制图像转换为 BASE64,然后再上传,然后将图像填充为作为突变参数传递的 GraphQLInputObjectType 上的 BASE64 字符串。在服务器上,该字段可以简单地保存在数据库列中。例如:

const UserInputType = new GraphQLInputObjectType({
  name: 'UserInput',
  description: 'The user of our system',

  fields: () => ({
    username: {
        type: GraphQLString,
        description: 'Name of the user'
    },
    image: {
        type: GraphQLString,
        description: 'Image of the user'
    }
  })
});

【讨论】:

    【解决方案2】:

    我最终使用了apollo-upload-client,效果很好!

    【讨论】:

    • 你能添加你的代码或github链接吗?将不胜感激!我发现的代码很少。谢谢!
    猜你喜欢
    • 2022-09-26
    • 2013-12-17
    • 2014-03-21
    • 2017-05-31
    • 2013-11-02
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多