Skip to content

Commit 414f09c

Browse files
SvelteKit file uploads (#8)
One example for file uploads with NodeJS, one with S3 --------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent 3598a5f commit 414f09c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3546
-63
lines changed

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
node_modules
1+
.DS_Store
2+
node_modules
3+
build
4+
.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
vite.config.js.timestamp-*
10+
vite.config.ts.timestamp-*
11+
.temp-files

examples/counter-library/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
},
2424
"devDependencies": {
2525
"@sveltejs/adapter-auto": "^2.0.0",
26-
"@sveltejs/kit": "^1.5.0",
26+
"@sveltejs/kit": "^1.12.0",
2727
"@sveltejs/package": "^2.0.0",
2828
"prettier": "^2.8.0",
2929
"prettier-plugin-svelte": "^2.8.1",
3030
"publint": "^0.1.9",
31-
"svelte": "^3.54.0",
31+
"svelte": "^3.57.0",
3232
"svelte-check": "^3.0.1",
3333
"tslib": "^2.4.1",
3434
"typescript": "^4.9.3",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
10+
# Ignore files for PNPM, NPM and YARN
11+
pnpm-lock.yaml
12+
package-lock.json
13+
yarn.lock
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['eslint:recommended', 'prettier'],
4+
plugins: ['svelte3'],
5+
overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
6+
parserOptions: {
7+
sourceType: 'module',
8+
ecmaVersion: 2020
9+
},
10+
env: {
11+
browser: true,
12+
es2017: true,
13+
node: true
14+
}
15+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
vite.config.js.timestamp-*
10+
vite.config.ts.timestamp-*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
10+
# Ignore files for PNPM, NPM and YARN
11+
pnpm-lock.yaml
12+
package-lock.json
13+
yarn.lock
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"useTabs": true,
3+
"singleQuote": true,
4+
"trailingComma": "none",
5+
"printWidth": 100,
6+
"plugins": ["prettier-plugin-svelte"],
7+
"pluginSearchDirs": ["."],
8+
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SvelteKit file uploads using Node.js
2+
3+
This example demonstrates how you can upload files with SvelteKit and Node.js in two different ways.
4+
5+
**Note:** This code makes use of the `Readable.fromWeb()` API to convert between web streams and Node.js streams so Node.js >= 17.0.0 is required.
6+
7+
Both forms write files to the local disk into a directory specified by the `FILES_DIR` environment variable. Uploaded files are served through the `src/routes/files/[name]/+server.js` endpoint.
8+
9+
## Form 1: Small files
10+
11+
This first form sends the file as `FormData` to the SvelteKit server.
12+
13+
- It works with and without JavaScript
14+
- It uses FormData and SvelteKit's form actions
15+
- It should only be used for small files such as avatar images because the whole file first needs to be parsed in memory with `event.request.formData()` and there is no upload progress indicator.
16+
17+
## Form 2: Small and large files
18+
19+
The second form for both small and large files uses a custom store and posts the raw file body to the `upload/+server.js` endpoint.
20+
21+
- JavaScript is required for this to work
22+
- A custom store handles the request and calculates the upload progress
23+
- `XMLHTTPRequest` is used to make the requests because `fetch` cannot be used (yet) to calculate the upload progress
24+
- The file object from the file input element is used as the body of the request
25+
- Additional information such as the file's name is passed to the server using custom request headers such as `x-file-name`
26+
- If a file with the same name has already been uploaded before the endpoint closes the connection by calling `event.body.cancel()`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true
12+
}
13+
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias and https://kit.svelte.dev/docs/configuration#files
14+
//
15+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
16+
// from the referenced tsconfig.json - TypeScript does not merge them in
17+
}

0 commit comments

Comments
 (0)