CREATE COLUMN TABLE "MY_SCHEMA"."TBL_ATTACHMENT"(
"ATTACHMENT_ID" INTEGER CS_INT NOT NULL,
"FILE_NAME" VARCHAR(256),
"FILE_DESC" VARCHAR(256),
"FILE_TYPE" VARCHAR(100),
"FILE_CONTENT_BIN" BLOB MEMORY THRESHOLD 1000
)
CREATE SEQUENCE "MY_SCHEMA"."TBL_ATTACHMENT_SEQ" START WITH 1001;
const hana = require('@sap/hdbext');
const multer = require('multer');
const multerMemoryStore = multer({ storage: multer.memoryStorage() });
function getNewAttachmentId() {
return new Promise(function (resolve, reject) {
let q = `SELECT
"MY_SCHEMA"."TBL_ATTACHMENT_SEQ".NEXTVAL
AS ID FROM DUMMY`;
hana.createConnection(config.db, function (err, client) {
if (err) {
reject(err);
return;
}
client.prepare(q, (err, statement) => {
if (err) {
reject(err);
client.close();
return;
}
statement.exec([],
function (err, rs) {
if (err) {
reject(err);
client.close();
return;
}
resolve(rs[0].ID);
client.close();
});
});
});
});
}
app.post('/api/v1/postattachmentinfo', [Auth Stuff here], function (req, res) {
getNewAttachmentId()
.then((iAttachmentId) => {
let query = `INSERT INTO "MY_SCHEMA"."TBL_ATTACHMENT" ` +
`(
"ATTACHMENT_ID",
"FILE_TYPE",
"FILE_NAME",
"FILE_DESC"
) ` +
`VALUES (
?, ?, ?, ?)`;
let params = [
iAttachmentId,
req.body.FILETYPE,
req.body.FILENAME,
req.body.FILEDESCRIPTION
];
hana.createConnection(config.db, function (err, client) {
if (err) {
debug(err);
res.status(503).send(err);
return;
}
client.prepare(query, (err, statement) => {
if (err) {
debug(err);
res.status(400).send(err);
return;
}
statement.exec(params,
function (err, rs) {
if (err) {
debug(err);
res.status(400).send(err);
return;
}
console.log(rs);
res.status(200).send({ iAttachmentId });
});
});
});
})
.catch((err) => {
res.status(500).send("An error occurred. " + err);
});
});
app.put('/api/v1/attachfilebin/:attachmentId',
[Auth Stuff Here], multerMemoryStore.single('att'), (req, res) => {
// multerMemoryStore.single('att') :
// the parameter must have the same value as in the file uploader control property name,
// in this example the Name of the FileUploader control is 'att'
// if there is a mismatch, multer will throw an error
return new Promise(function (resolve, reject) {
let query = `UPDATE "MY_SCHEMA"."TBL_ATTACHMENT"
SET "FILE_CONTENT_BIN" =?
WHERE ATTACHMENT_ID=${req.params.attachmentId}`;
let buff = req.file.buffer;
hana.createConnection(config.db, function (err, client) {
if (err) {
res.status(400).send(err);
reject(err);
return;
}
client.prepare(query, (err, statement) => {
if (err) {
res.status(400).send(err);
reject(err);
return;
}
statement.exec([buff], function (err, rs) {
if (err) {
res.status(400).send(err);
reject(err);
return;
}
res.status(200).send("OK");
resolve();
});
});
});
});
});
app.get('/api/v1/getAttachmentbin/:attachmentId', function (req, res) {
return new Promise(function (resolve, reject) {
let q = `SELECT
"FILE_CONTENT_BIN" FROM "MY_SCHEMA"."TBL_ATTACHMENT"
WHERE "ATTACHMENT_ID" = '${req.params.attachmentId}' `;
hana.createConnection(config.db, function (err, client) {
if (err) {
res.status(400).send(err);
reject(err);
return;
}
client.prepare(q, (err, statement) => {
if (err) {
res.status(400).send(err);
reject(err);
client.close();
return;
}
statement.exec([],
function (err, rs) {
if (err) {
res.status(400).send(err);
reject(err);
client.close();
return;
}
let bufDec = rs[0].FILE_CONTENT_BIN;
res.status(200).send(bufDec);
resolve();
client.close();
});
});
});
});
});
app.delete('/api/v1/removeattachment/:attachmentId',
[Auth Stuff Here], function (req, res) {
var q = `DELETE FROM "MY_SCHEMA"."TBL_ATTACHMENT" ` +
`WHERE "ATTACHMENT_ID" = ?`;
var params = req.params.attachmentId;
hana.createConnection(config.db, function (err, client) {
if (err) {
res.status(400).send(err);
return;
}
client.prepare(q, (err, statement) => {
if (err) {
res.status(400).send(err);
return;
}
statement.exec([params], function (err, rs) {
if (err) {
res.status(400).send(err);
return;
}
res.status(200).send("OK");
return;
});
});
});
});
<mvc:View
controllerName="sap.ui.unified.sample.FileUploaderBasic.Controller"
xmlns:l="sap.ui.layout"
xmlns:u="sap.ui.unified"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
class="viewPadding">
<l:VerticalLayout>
<u:FileUploader
id="fileUploader"
name="att" // this is the same name in multer parameters ... if they don't match you will get an error
uploadUrl="" // we will build the uploadUrl at runtime
httpRequestMethod="PUT"
maximumFileSize = 10 // more than this can crash the nodejs app
tooltip="Upload your file to the local server"
multiple=false
sendXHR=true,
useMultipart=true,
uploadComplete="handleUploadComplete"/>
<Button
text="Upload File"
press="doAddAttachmentInfo"/>
</l:VerticalLayout>
</mvc:View>
doAddAttachmentInfo: async function (oEvent) {
let that = this;
let sFileName = sap.ui.getCore().getControl("fileUploader").getValue();
let sFileType = sap.ui.getCore().getControl("FileType").getFileType();
let sFileDescription = sap.ui.getCore().getControl("FileDescription").getValue(); // this control is not in the sample 🙂
if (sFileName === '') {
return;
}
let oFormData = {};
oFormData.FILETYPE = sFileType;
oFormData.FILENAME = sFileName;
oFormData.FILEDESCRIPTION = sFileDescription;
var token = getCSRFToken();
$.ajax({
type: "POST",
url: "/api/v1/attachmentinfo",
beforeSend: function (request) {
request.setRequestHeader('x-csrf-token', token);
},
data: oFormData,
success: function (res) {
let sAttachmentId = res.iAttachmentId;
that.doAddAttachFile(sAttachmentId);
},
error: function (jqXHR, textStatus, errorThrown) {
sap.m.MessageToast.show("Error: " + textStatus + " - " + errorThrown);
}
});
},
doAddAttachFile: async function() {
let oFileUploader = sap.ui.getCore().getControl("fileUploader");
let sUploadUrl = `/api/v1/attachfilebin`;
let token = getToken();
let headerParma = new sap.ui.unified.FileUploaderParameter();
headerParma.setName('x-csrf-token');
headerParma.setValue(token);
oFileUploader.addHeaderParameter(headerParma);
oFileUploader.setUploadUrl(sUploadUrl);
oFileUploader.upload();
},
function getToken() {
var token = null;
$.ajax({
url: '/api/v1/token',
type: "GET",
async: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRF-Token", "Fetch");
},
complete: function(xhr) {
token = xhr.getResponseHeader("X-CSRF-Token");
}
});
return token;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
11 | |
10 | |
8 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 |