Files
WarpBox/static/popups/examples.html
2026-04-29 02:35:23 +03:00

103 lines
3.1 KiB
HTML

<h3>Upload examples</h3>
<h4>Basic CLI upload</h4>
<pre class="code-block"><code>curl \
-F 'files=@./photo.png' \
-F 'retention=24h' \
{{ origin }}/upload
</code></pre>
<h4>Multiple files with password</h4>
<pre class="code-block"><code>curl \
-F 'files=@./one.png' \
-F 'files=@./two.zip' \
-F 'retention=1h' \
-F 'password=secret-pass' \
{{ origin }}/upload
</code></pre>
<h4>Go</h4>
<pre class="code-block"><code>package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
file, err := os.Open("photo.png")
if err != nil { panic(err) }
defer file.Close()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("files", "photo.png")
if err != nil { panic(err) }
if _, err := io.Copy(part, file); err != nil { panic(err) }
_ = writer.WriteField("retention", "1h")
writer.Close()
req, err := http.NewRequest("POST", "{{ origin }}/upload", &body)
if err != nil { panic(err) }
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if err != nil { panic(err) }
defer resp.Body.Close()
out, _ := io.ReadAll(resp.Body)
fmt.Println(string(out))
}
</code></pre>
<h4>Java 11+ HttpClient</h4>
<pre class="code-block"><code>import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
public class UploadWarpBox {
public static void main(String[] args) throws Exception {
String boundary = "----WarpBoxBoundary" + System.currentTimeMillis();
Path file = Path.of("photo.png");
byte[] prefix = ("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"retention\"\r\n\r\n" +
"1h\r\n" +
"--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"files\"; filename=\"photo.png\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n").getBytes();
byte[] suffix = ("\r\n--" + boundary + "--\r\n").getBytes();
byte[] fileBytes = Files.readAllBytes(file);
byte[] body = new byte[prefix.length + fileBytes.length + suffix.length];
System.arraycopy(prefix, 0, body, 0, prefix.length);
System.arraycopy(fileBytes, 0, body, prefix.length, fileBytes.length);
System.arraycopy(suffix, 0, body, prefix.length + fileBytes.length, suffix.length);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("{{ origin }}/upload"))
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
.build();
HttpResponse&lt;String&gt; response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
</code></pre>
<h4>JavaScript Node.js</h4>
<pre class="code-block"><code>import { openAsBlob } from 'node:fs';
const file = await openAsBlob('./photo.png');
const form = new FormData();
form.append('files', file, 'photo.png');
form.append('retention', '1h');
const res = await fetch('{{ origin }}/upload', {
method: 'POST',
body: form
});
console.log(await res.text());
</code></pre>