如何上传图片到strapi?
How to upload image to strapi?
如何将图片上传到 Strapi 服务器?
- 我使用 GraphQL 插件。
- 我更喜欢所有 API
的单一端点
I found an article about how to manage file uploads but I have some questions
我需要将图像转换为 Base64 字符串吗?
我的生产服务器将是 PostgreSQL。我打算将图像存储为 Blob。但现在事实证明我只能将表单数据发送到 Strapi 服务器。
我需要类似 apollo-upload-client 的东西吗?
注意:目前我使用vue-apollo和nativescript-vue作为前端。
import VueApollo from "vue-apollo";
import { ApolloClient, InMemoryCache, HttpLink } from "apollo-boost";
import { setContext } from "apollo-link-context";
如果您尝试使用 GraphQL 上传文件,我建议您查看此要点 - https://gist.github.com/alexandrebodin/fedc71c8513bfbb6283cc90ae62755c5
您应该拥有实现该目标所需的所有信息。
感谢@Jim LAURIE 的回答,我让我的节点工作了:
import { GraphQLClient, gql } from "graphql-request"
import { createReadStream } from "fs";
[...]
const endpoint = 'http://localhost:1337/graphql';
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: `Bearer ${jwt}`,
},
});
try {
const data = await graphQLClient.request( gql`
mutation($file: Upload!) {
upload(file: $file) {
id
name
}
}
`, {
file: createReadStream(__dirname + '/Strapi/test/picture.jpg') // ⚠ Ensure path is correct or the stream will never send data and you will have Socket Hang out error
});
console.info("ID of file:" + data.upload.id);
console.info(data)
console.info("data")
} catch (error) {
console.error(error)
console.error("error")
}
如果您不知道如何获取 JWT check the units testing docs of Strapi,转换为 GraphQL 应该很容易。
如何将图片上传到 Strapi 服务器?
- 我使用 GraphQL 插件。
- 我更喜欢所有 API 的单一端点
I found an article about how to manage file uploads but I have some questions
我需要将图像转换为 Base64 字符串吗?
我的生产服务器将是 PostgreSQL。我打算将图像存储为 Blob。但现在事实证明我只能将表单数据发送到 Strapi 服务器。
我需要类似 apollo-upload-client 的东西吗?
注意:目前我使用vue-apollo和nativescript-vue作为前端。
import VueApollo from "vue-apollo";
import { ApolloClient, InMemoryCache, HttpLink } from "apollo-boost";
import { setContext } from "apollo-link-context";
如果您尝试使用 GraphQL 上传文件,我建议您查看此要点 - https://gist.github.com/alexandrebodin/fedc71c8513bfbb6283cc90ae62755c5
您应该拥有实现该目标所需的所有信息。
感谢@Jim LAURIE 的回答,我让我的节点工作了:
import { GraphQLClient, gql } from "graphql-request"
import { createReadStream } from "fs";
[...]
const endpoint = 'http://localhost:1337/graphql';
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: `Bearer ${jwt}`,
},
});
try {
const data = await graphQLClient.request( gql`
mutation($file: Upload!) {
upload(file: $file) {
id
name
}
}
`, {
file: createReadStream(__dirname + '/Strapi/test/picture.jpg') // ⚠ Ensure path is correct or the stream will never send data and you will have Socket Hang out error
});
console.info("ID of file:" + data.upload.id);
console.info(data)
console.info("data")
} catch (error) {
console.error(error)
console.error("error")
}
如果您不知道如何获取 JWT check the units testing docs of Strapi,转换为 GraphQL 应该很容易。