(test) nodejs server (express + multer + zlib)
On the premise of not be compressed, can normal to upload files
The client c # code summary
String filePath="SOME FILE PATH";
Using (var httpClient=new httpClient ())
{
Using (var form=new MultipartFormDataContent ())
{
Using (var streamContent=new streamContent (File. OpenRead (filePath)))
{
Form. The Add (streamContent, "file", Path. The GetFileName (filePath));
HttpResponseMessage response=httpClient. PostAsync (url, form). The Result;
}
}
}
Nodejs code on the server profile
Var storage=multer. MemoryStorage ()
Var upload=multer ({
Storage: storage
})
App. Post ('/compress, upload. Single (' file '), async (the req, res)=& gt; {
Try {
The console. The log (` file: ${JSON. Stringify (the req. File)} `);
.
}
.
}
Then try to Gzip compression
The client c # code summary
- to make a first class compression with the expansion of the
Public class GzipCompressedContent: HttpContent
{
Private readonly HttpContent _content;
Public GzipCompressedContent (HttpContent content)
{
//Copy the original headers
The foreach (KeyValuePairThe header in the Headers)
{
Headers. TryAddWithoutValidation (header. The Key, the header Value);
}
Headers. ContentEncoding. Add (" gzip ");
_content=content;
}
Protected override async Task SerializeToStreamAsync (Stream, Stream, TransportContext context)
{
Using (var gzip=new GZipStream (stream, CompressionMode.Com press, true))
{
//Compress the - the original content
Await _content. CopyToAsync (gzip);
}
}
Protected override bool TryComputeLength (out long length)
{
//the Content - Lenght is optional, so set to 1
Length=1;
return false;
}
}
- before the API call is compressed
String filePath="SOME FILE PATH";
Using (var httpClient=new httpClient ())
{
Using (var form=new MultipartFormDataContent ())
{
Using (var streamContent=new streamContent (File. OpenRead (filePath)))
{
Form. The Add (streamContent, "file", Path. The GetFileName (filePath));
//Gzip compression
Using (var compressed=new GzipCompressedContent (form))
{
HttpResponseMessage response=httpClient. PostAsync (url, compressed). The Result;
}
}
}
}
Server side in the case of not modified, can not receive files, should can't be automatic processing Gzip Request,
So using zlib increased the the following code
Var express=the require (' express ');
Var app=express ();
The function gUnzip (the req, res, next) {
Var newReq;
If (the req. Headers [' content - encoding]==='gzip') {
The console. The log (" received gzipped body ");
NewReq=the req. Pipe (zlib. CreateGunzip ());
Object. GetOwnPropertyNames (newReq). ForEach (function (p) {
The req [p]=newReq [p].
});
}
The next ();
}
App. Use (gUnzip);
See the log decompression is run by, but the file still have not received,
Refer to
The practice of c # compression, right?
The client and the server which side has a problem, don't know how to look up,,,
CodePudding user response:
The