简体   繁体   中英

File upload without library

I'm looking for how to upload files using only Node.js. Every tutorial or example I got off Google uses formidable or express. But I'd like an example that isn't using any libraries.

When I upload a file, I get this as the request data:

{
    post: {
        '------WebKitFormBoundarySUszotMiE1sXRapA\r\nContent-Disposition: form-data; name': '"file"; filename="img not found.png"\r\nContent-Type: image/png\r\n\r\n�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0000\u0012\u0000\u0000\u0000\u0014\b\u0002\u0000\u0000\u0000\u000f��\u001d\u0000\u0000\u0000\u0019tEXtSoftware\u0000Adobe ImageReadyq�e<\u0000\u0000\u0003$iTXtXML:com.adobe.xmp\u0000\u0000\u0000\u0000\u0000<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27        "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmp:CreatorTool="Adobe Photoshop CS6 (Macintosh)" xmpMM:InstanceID="xmp.iid:51B0B079D16D11E1800BF631A3922565" xmpMM:DocumentID="xmp.did:51B0B07AD16D11E1800BF631A3922565"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:51B0B077D16D11E1800BF631A3922565" stRef:documentID="xmp.did:51B0B078D16D11E1800BF631A3922565"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>�\u0003�\u0006\u0000\u0000\u0000�IDATxڔ�\u0001\u000e� \b��y/��z2�d=�E��[�\u001c�>�\u001f\f����Ez\u0011�\u0013�{�//�VJ\u0011���a\u001b}\u0015d\u0014ѭ�RJD�*�͆\r�QI\u0013\u001b�y(K�s��$GI���\u0004�5Z\u0001�H\u0013k#E��;)�\r ½\u0017XU�6X[(箒\u001c�\u001c�\u0014��4`',
        'Ù^�a���$�#': '',
        'AJ\u0012���\u0006�\r\u0010\u001aR�N�\u000b\u0013���`T��k�@�V�ߏ�}��\'�\u0000\u0017gp��\u0002��\u0000\u0000\u0000\u0000IEND�B`�\r\n------WebKitFormBoundarySUszotMiE1sXRapA--\r\n': ''
    }
}

When uploading a simple image. I can see that all the data is received. But I just have no clue about how to handle it.

Any idea, or suggestion is welcome.

Thanks!

The simplest answer is of course to just use Express/Connect/Formidable (from most abstract to most direct solution).

You can also do this yourself by checking out the source of:

I have seen no other direct implementation that parses request bodies, and file uploads. So your option is to go read the source and recreate these, or just use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM