110,528
社区成员
发帖
与我相关
我的任务
分享
string filePath = "SOME FILE PATH";
using (var httpClient = new HttpClient())
{
using (var form = new MultipartFormDataContent())
{
using (var streamContent = new StreamContent(File.OpenRead(filePath)))
{
form.Add(streamContent, "file", Path.GetFileName(filePath));
HttpResponseMessage response = httpClient.PostAsync(url, form).Result;
}
}
}
var storage = multer.memoryStorage()
var upload = multer({
storage: storage
})
app.post('/compress', upload.single('file'), async (req, res) => {
try {
console.log(` file: ${JSON.stringify(req.file)}`);
...
}
...
}
public class GzipCompressedContent : HttpContent
{
private readonly HttpContent _content;
public GzipCompressedContent(HttpContent content)
{
// Copy original headers
foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
{
Headers.TryAddWithoutValidation(header.Key, header.Value);
}
Headers.ContentEncoding.Add("gzip");
_content = content;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using (var gzip = new GZipStream(stream, CompressionMode.Compress, true))
{
// Compress the entire original content
await _content.CopyToAsync(gzip);
}
}
protected override bool TryComputeLength(out long length)
{
// Content-Lenght is optional, so set to -1
length = -1;
return false;
}
}
string filePath = "SOME FILE PATH";
using (var httpClient = new HttpClient())
{
using (var form = new MultipartFormDataContent())
{
using (var streamContent = new StreamContent(File.OpenRead(filePath)))
{
form.Add(streamContent, "file", Path.GetFileName(filePath));
// Gzip压缩
using (var compressed = new GzipCompressedContent(form))
{
HttpResponseMessage response = httpClient.PostAsync(url, compressed).Result;
}
}
}
}
var express = require('express');
var app = express();
function gUnzip(req, res, next) {
var newReq;
if (req.headers['content-encoding'] === 'gzip') {
console.log("received gzipped body");
newReq = req.pipe(zlib.createGunzip());
Object.getOwnPropertyNames(newReq).forEach(function (p) {
req[p] = newReq[p];
});
}
next();
}
app.use(gUnzip);