Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/models/code/TextToImageCode.astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export default {
prompt: "cyberpunk cat",
};

const response = await env.AI.run(
const stream = await env.AI.run(
"${name}",
inputs
);

return new Response(response, {
headers: {
"content-type": "image/png",
"content-type": "image/jpg",
},
});
},
Expand Down
56 changes: 56 additions & 0 deletions src/content/docs/images/upload-images/upload-file-worker.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
pcx_content_type: how-to
title: Upload via a Worker

---

You can use a Worker to upload your image to Cloudflare Images.

Refer to the example below or refer to the [Workers documentation](/workers/) for more information.

```ts
const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1";
const TOKEN = "<YOUR_TOKEN_HERE>";

const image = await fetch("https://example.com/image.png");
const bytes = await image.bytes();

const formData = new FormData();
formData.append('file', new File([bytes], 'image.png'));

const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${TOKEN}`,
},
body: formData,
});
```

## Upload from AI generated images

To upload an AI generated image, refer to the example below.

```ts
const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1";
const TOKEN = "YOUR_TOKEN_HERE";

const stream = await env.AI.run(
"@cf/bytedance/stable-diffusion-xl-lightning",
{
prompt: YOUR_PROMPT_HERE
}
);
const bytes = await (new Response(stream)).bytes();

const formData = new FormData();
formData.append('file', new File([bytes], 'image.jpg');

const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${TOKEN}`,
},
body: formData,
});
```