Upload image url

System Information
  • Strapi Version: 3.3.3
  • Operating System: Windows 10
  • Database: Mongo DB
  • Node Version: 14
  • NPM Version: 6

I am working on a migration from our current CMS to Strapi.

Is there a way to post images to ‘/upload’ from an external URL? If not, what is the best way to post an image as a blob via XHR?

Thanks in advance,

You mean like this?

 const uploadByUrl = async () => {
    URL = "https://images.unsplash.com/photo-1611095785020-1ba3dd228ea7?ixid=MXwxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHwxfHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=900&q=60"; // <- image url
    fetch(URL)
      .then(response => response.blob())
      .then(function (myBlob) {
        const formData = new FormData();
        formData.append('files', myBlob);
        console.log(formData)
        fetch('http://localhost:1337/upload', {
          method: 'POST',
          headers: {
            "Authorization": "Bearer ", // <- Don't forget Authorization header if you are using it.
          },
          body: formData,
        }).then((response) => {
          const result = response.json()
          console.log("result", result)
        }).catch(function (err) {
          console.log("error:");
          console.log(err)
        });
      })
  }
  uploadByUrl()

Hi,

this looks very promising!

I’m looking into a way to use this approach in the outdated strapi-plugin-import-content plugin that I’m trying to fix. I just don’t feel comfortable calling this method IN BULK, let’s say in batches of 50: this will download 50 images at once, create 50 FormData objects containing the binary data of these images and do 50 POST requests to the content-manager endpoints…

I’m also not sure how to get the authorization info in a server-side process to avoid allowing access to the image upload endpoint to the public. @kuwaitbinary Can you maybe share your vision on how to achieve this? See also Need help with reviving strapi-plugin-import-content for some more background info.

Thanks in advance!

As far as I know there is no update in bulk / updateMany yet according to this

I am still researching for createMany

The admin panel login for strapi is

// Login admin (POST http://localhost:1337/admin/login)
fetch("http://localhost:1337/admin/login", {
      "method": "POST",
      "headers": {
            "Content-Type": "application/json; charset=utf-8"
      },
      "body": "{\"email\":\"admin@example.com\",\"password\":\"pass\"}"
})
.then((res) => res.text())
.then(console.log.bind(console))
.catch(console.error.bind(console));

I assume you know the regular login method

169.254.37.16:1337/auth/local

Maybe later on I will try to re-visit the link you posted and think of idea how to use the plugin to upload many images. @4levels