expressjs / expressjs/multer

XSD file always corrupt when uploaded to server

Open
#595 6 comments 0 reactions 0 assignees View on GitHub
investigate
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Hello, sorry if this duplicate.
So my server code always gave me the corrupt xsd file.
So here is my server code.

``` javascript
var myCustomStorage = require('./custom-storage');
var storage = myCustomStorage({
destination: function (req, file, cb) {
cb(null, './files/' + Date.now() + file.originalname)
}
});

var cpUpload = multer({ storage: storage, limits: {fieldSize: 10000000, fieldNameSize: 10000000}}).single('schema-file');

var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
next();
});
app.use(cpUpload);
app.use(timeout(200000));
app.use(haltOnTimedout);

function haltOnTimedout(req, res, next){
if (!req.timedout){
next();
}else{
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({
status: 'SUCCESS 1',
description: 'success'
}));
}
}

app.post('/ws-api-designer-upload-files', cpUpload, function (req, res, next) {
cpUpload(req,res,function(err){
if(err){
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({
status: 'ERROR 1',
description: err.message
}));
return;
}else{
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({
status: 'SUCCESS 1',
description: 'success'
}));
return;
}
});
});
```
front-end code
``` javascript
postFile(fileToUpload: File)/*: Observable*/ {
const endpoint = 'http://localhost:3000/ws-api-designer-upload-files';
const file: FormData = new FormData();
file.append('schema-file', fileToUpload, fileToUpload.name);
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/x-www-form-urlencoded');

return new Promise((resolve, reject) => {
this.http.
post(endpoint, file, { headers: headers })
.subscribe(res => {
resolve(res);
}, (err) => {
reject(err);
});
});
}
```

When i execute by uploading the xsd file it says success. But when i compare the [original file ](https://i.stack.imgur.com/GfGg0.png)and [uploaded file](https://i.stack.imgur.com/URapv.png) are not the same.
[This is the original file](https://pastebin.com/RsEeY5fJ)
[This is the uploaded file](https://pastebin.com/2xfe5yds)
Any help would be appreciated!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.