deligenius / multiparser Goto Github PK
View Code? Open in Web Editor NEWmultipart/form-data parser for Deno servers
multipart/form-data parser for Deno servers
Does this module work with opine. If so how would one implement it to handle file uploading.
If you send a form and use multiparser, it works great. If you send files or in my case an image the image is visible on the form but you can't store said files on the server. Thats where upload comes in. If i use upload by itself the image gets stored on the server.
Let me show you my route
router.post('/feed/post',upload('images', ['jpg','png','jpeg'], 20000000, 10000000, true, false, true),createPost);
The upload comes from this url
import { upload, preUploadValidate} from "https://deno.land/x/upload_middleware_for_oak_framework/mod.ts";
If I use upload alone, I can save the image or file on the server on the specified folder. If I just use multiparse without upload middleware I can see the entire form information.
The problem is that I can't use them both.
`export const createPost = async (ctx:any)=>{
try{
const data = await ctx.request.body();
console.log(JSON.stringify(data));
const form = JSON.stringify(await multiParser(ctx.request.serverRequest));
const body = JSON.parse(form);
console.log(body.imageUrl.filename);
console.log(body.title);`
This is the error I get.
Listening on: http://localhost:5000 OPTIONS http://localhost:5000/feed/post - Wed Jul 15 2020 12:35:39 GMT-0400 (EDT) {"type":"form-data","value":{}} POST http://localhost:5000/feed/post - Wed Jul 15 2020 12:35:39 GMT-0400 (EDT) error: Uncaught UnexpectedEof: throw new Deno.errors.UnexpectedEof(); ^ at MultipartReader.nextPart (multipart.ts:371:15) at async MultipartReader.readForm (multipart.ts:286:17) at async multiParser.ts:23:24 [denon] app crashed - waiting for file changes before starting ...
async function parseFormData(contentType: string, request: ServerRequest) {
const boundaryReg = /boundary=(.+)/i;
const match = contentType.match(boundaryReg);
const boundary = match?.[1];
const res: {[key: string]: string | FormFile} = {};
if(boundary){
// const mr = new MultipartReader(request.r, boundary);
// const form = await mr.readForm(2 * 1024 * 1024);
const form: any = await multiParser(request);
if(form){
for(const [key, value] of form.entries()){
const newValue: any = value || {};
const val = <FormFile>value;
if(val.content){
const decoder = new TextDecoder();
newValue.value = decoder.decode(val.content);
}
res[key] = <string | FormFile>newValue;
}
}
}
return res;
}
const originRequest = ctx.request.serverRequest;
const contentType = ctx.request.headers.get('content-type') || 'text';
// set params data
ctx.query = parseQuery(ctx.request.url.search.slice(1));
// init request body
const body = {type: '', value: {}};
if(ctx.request.hasBody){
// field to save body
let _body: any;
// field to save type
let type: string;
const contentTypeFilter = getContentType(contentType);
// deal with form-data first
if(contentTypeFilter.isFormData){
_body = await parseFormData(contentType, originRequest);
type = 'formData';
}
}
This is the result and some information
I think this question casued by
const form = await multiParser(request);
If I delete it, it worked
Please update Multiparser std to [email protected], yet you are still using [email protected].
It's could bring a lot of bugs..
import { multiParser } from 'https://deno.land/x/multiparser/mod.ts'
import Subscriptions from '../models/subscriptions.ts';
class Controller {
async post(context: any) {
const body = await multiParser(context.request.serverRequest);
// const existing = await Subscriptions.where('email', body.email).get()
const existing = [];
console.log(body);
if (existing.length) {
return (context.response.body = "Email already exists");
}
const subscription = {};
// const subscription = await Subscriptions.create({
// id: crypto.randomUUID(),
// email: body.email,
// });
context.response.body = subscription;
}
}
export default new Controller();
I get error:
[uncaught oak error]: TypeError - Cannot read property 'headers' of undefined
TODO:
Trying to use this but I get the following error:
error: TS2345 [ERROR]: Argument of type 'import("https://deno.land/[email protected]/http/server").ServerRequest' is not assignable to parameter of type 'import("https://deno.land/[email protected]/http/server").ServerRequest'.
Types of property 'r' are incompatible.
Type 'import("https://deno.land/[email protected]/io/bufio").BufReader' is not assignable to type 'import("https://deno.land/[email protected]/io/bufio").BufReader'.
Types have separate declarations of a private property 'buf'.
const form = await multiParserV2(req, {})
It seems the lib hard-codes the std lib in mod.ts, and it has not been updated since 0.61. Current version appears to be 0.74.
Hello
I'm trying to use multiparser, like so:
import { multiParser, FormFile } from 'https://deno.land/x/[email protected]';
But get Import 'https://deno.land/x/[email protected]' failed, not found.
Hello. I've updated to Deno 1.2 but it gives the following errors.
`error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
Type 'URL' is not assignable to type 'string'.
return new URL(url).pathname
~~~
at https://deno.land/[email protected]/path/win32.ts:917:18
TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
Type 'URL' is not assignable to type 'string'.
return new URL(url).pathname;
~~~
at https://deno.land/[email protected]/path/posix.ts:438:18
Found 2 errors.`
Can you please help me on this. This way I can keep using your code.
Thanks!
I recently switched from nodejs to deno and came to use this library for form parsing as alternative to formiddable. It seems that there is no filepath property for a file if sent from a form. Why is that ? Formiddable seems to have it and this is now not allowing me to read file and send as buffer. i have tried working with Uint8array but sending it as base64 has some complications that dont get resolved.
I use this parser. But it has no response.
I used postman to send multipart/form-data request, but it was sending forever and never stop.
I think it had no condition to stop read? or other reason?
The output terminal had no error message. If I cancel the request, It showed error.
I think thats the same question here denoland/deno#5830
Oak uses std 0.95.0, which means multiparser can't be used since it relies on 0.89. It just doesn't work.
This prevents us from making a necessary Deno upgrade
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.