|
| 1 | +Here's a more concise version of the documentation with a focus on examples: |
| 2 | + |
| 3 | +###Example |
| 4 | + |
| 5 | +To upload files on the front end, you can either use a form action or direct call a server action |
| 6 | + |
| 7 | +####Form action |
| 8 | + |
| 9 | +```tsx |
| 10 | +// UploadFileViaForm.tsx |
| 11 | +'use client'; |
| 12 | +import {uploadFile }from'./file.action'; |
| 13 | + |
| 14 | +exportfunction UploadFileViaForm() { |
| 15 | +return ( |
| 16 | +<formaction={uploadFile}> |
| 17 | +{/** The name attribute must match the server action input name*/} |
| 18 | +<inputtype="file"name="file" /> |
| 19 | +<buttontype="submit">Upload via form action</button> |
| 20 | +</form> |
| 21 | +); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +####Direct call |
| 26 | + |
| 27 | +```tsx |
| 28 | +// UploadFileViaActionCall.tsx |
| 29 | +'use client'; |
| 30 | +import {objectToFormData }from'@davstack/action'; |
| 31 | +import {uploadFile }from'./file.action'; |
| 32 | + |
| 33 | +exportdefaultfunction UploadFileViaActionCall() { |
| 34 | +return ( |
| 35 | +<button |
| 36 | +onClick={async ()=> { |
| 37 | +const file=newBlob([], { type:'text/plain' }); |
| 38 | +// must convert object to form data if using direct call |
| 39 | +awaituploadFile(objectToFormData({file })); |
| 40 | +}} |
| 41 | +> |
| 42 | +upload via direct action call |
| 43 | +</button> |
| 44 | +); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +When directly calling a server action is is essential to wrap the input in`objectToFormData` as otherwise next.js will not send the file to the backend. |
| 49 | + |
| 50 | +####Backend |
| 51 | + |
| 52 | +```ts |
| 53 | +// file.action.ts |
| 54 | +'use server'; |
| 55 | +import {action,zodFile }from'@davstack/action'; |
| 56 | + |
| 57 | +exportconst uploadFile=action() |
| 58 | +.input({ |
| 59 | +file:zodFile({ type:'image/*' }), |
| 60 | +}) |
| 61 | +.mutation(async ({input,ctx })=> { |
| 62 | +console.log('FILE UPLOADING!'); |
| 63 | +const file=input.file; |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +Under the hood, davstack action converts formdata to objects so you can use the input as a normal object, no need for`formData.get`. |
| 68 | + |
| 69 | +####`zodFile` Usage |
| 70 | + |
| 71 | +```ts |
| 72 | +// Accept only image files |
| 73 | +const imageFile=zodFile({ type:'image/*' }); |
| 74 | + |
| 75 | +// Accept only audio files |
| 76 | +const audioFile=zodFile({ type:'audio/*' }); |
| 77 | + |
| 78 | +// Accept only video files |
| 79 | +const videoFile=zodFile({ type:'video/*' }); |
| 80 | + |
| 81 | +// Accept only PDF files |
| 82 | +const pdfFile=zodFile({ type:'application/pdf' }); |
| 83 | + |
| 84 | +// Accept only text files |
| 85 | +const textFile=zodFile({ type:'text/*' }); |
| 86 | + |
| 87 | +// Accept specific file types |
| 88 | +const specificTypes=zodFile({ type: ['image/jpeg','image/png'] }); |
| 89 | + |
| 90 | +// Accept custom file type |
| 91 | +const customType=zodFile({ type:'application/custom' }); |
| 92 | + |
| 93 | +// Set maximum file size (in bytes) |
| 94 | +const limitedSize=zodFile({ type:'*', maxSizeMb:5 }); |
| 95 | +``` |
| 96 | + |
| 97 | +The`zodFile` validator allows you to specify the expected file type and maximum size: |
| 98 | + |
| 99 | +- Use`type: 'image/*'`,`type: 'audio/*'`,`type: 'video/*'`, etc., to accept files of a specific category. |
| 100 | +- Use`type: 'application/pdf'`,`type: 'text/*'`, etc., to accept files of a specific type. |
| 101 | +- Use an array of types, like`type: ['image/jpeg', 'image/png']`, to accept multiple specific types. |
| 102 | +- Use a custom type string, like`type: 'application/custom'`, to accept a custom file type. |
| 103 | + |
| 104 | +The`zodFile` validator returns a`ZodFile` object with properties and methods for working with the uploaded file. |