we are created folder in MID Server , in that folder create Zip files or PDF files, after sending files we want delete that files in MID Server?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 03:57 AM
we are created folder in MID Server , in that folder create Zip files or PDF files, after sending files we want delete that files in MID Server?
Plese find below i am shaaring code:
Note:Below code comment Zip_file.delete()-- we are enabled that time it's showing please check once and help me sir.
CODE MID Server Script Include:
var FileZipUtil = Class.create();
FileZipUtil.prototype = {
File: Packages.java.io.File,
FileInputStream: Packages.java.io.FileInputStream,
FileOutputStream: Packages.java.io.FileOutputStream,
ZipEntry: Packages.java.util.zip.ZipEntry,
ZipInputStream: Packages.java.util.zip.ZipInputStream,
JavaString: Packages.java.lang.String,
Base64: Packages.org.apache.commons.codec.binary.Base64,
FileUtils: Packages.org.apache.commons.io.FileUtils,
initialize: function() {
//gs.log("log 1 Initialize");
this.verbose = probe.getParameter("verbose");
this.file_name = probe.getParameter("file_name");
this.encoded_data = probe.getParameter("encoded_data");
this.request_type = probe.getParameter("request_type");
this.requested_file_path = probe.getParameter("requested_file_path");
this.result = {
messages: [],
files: [],
bd_transaction_sys_id: probe.getParameter("bd_transaction_sys_id"),
request_type: this.request_type
};
},
execute: function() {
try {
this.debug("here is the debug message " + this.request_type);
if (this.request_type == "initiate_file_transfer") {
//log
var destDir = new this.File("." + this.File.separator + "BD_processor");
//gs.log("output" +destDir);
if (!destDir.exists())
destDir.mkdir();
var data = this.Base64.decodeBase64(this.encoded_data);
var stream = new this.FileOutputStream(destDir.getCanonicalPath() + this.File.separator + this.file_name);
stream.write(data);
stream.close();
var fileZip = destDir.getCanonicalPath() + this.File.separator + this.file_name;
var empty_str = new Packages.java.lang.String("test");
var buffer = empty_str.getBytes();
var zis = new this.ZipInputStream(new this.FileInputStream(fileZip));
var zipEntry = zis.getNextEntry();
this.debug('before Zip entry');
while (zipEntry != null) {
this.debug('zipEntry is not null'+zipEntry.getName());
if (zipEntry.getName().indexOf(".xlsx") > -1) {
var newFile = this.newFile(destDir, zipEntry);
var fos = new this.FileOutputStream(newFile);
var len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
var zip_file = new this.File(destDir, zipEntry.getName());
var fileContent = this.FileUtils.readFileToByteArray(zip_file);
var encodedString = this.Base64.encodeBase64String(fileContent);
this.result.files.push({
file_name: zipEntry.getName(),
file_path: zip_file.getCanonicalPath()
});
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
// var downloaded_zip = new this.File(destDir.getCanonicalPath() + this.File.separator + this.file_name);
// downloaded_zip.delete();
} else if (this.request_type == "get_file_content") {
var zip_file = new this.File(this.requested_file_path);
var fileContent = this.FileUtils.readFileToByteArray(zip_file);
var encodedString1 = this.Base64.encodeBase64String(fileContent);
this.result.files.push({
file_name: this.file_name,
file_content: encodedString1
});
// zip_file.delete();
}
} catch (e) {
this.debug(e.toString());
}
return JSON.stringify(this.result);
},
newFile: function(destinationDir, zipEntry) {
var destFile = new this.File(destinationDir, zipEntry.getName());
return destFile;
},
debug: function(m) {
if (this.verbose == "true") {
ms.log(m);
this.result.messages.push(m);
}
},
type: 'FileZipUtil'
};
Thanks
Ram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 04:50 AM
Hi Ram,
update the function below
File: Packages.java.io.File,
FileInputStream: Packages.java.io.FileInputStream,
FileOutputStream: Packages.java.io.FileOutputStream,
ZipEntry: Packages.java.util.zip.ZipEntry,
ZipInputStream: Packages.java.util.zip.ZipInputStream,
JavaString: Packages.java.lang.String,
Base64: Packages.org.apache.commons.codec.binary.Base64,
FileUtils: Packages.org.apache.commons.io.FileUtils,
Files: Packages.java.nio.file.Files,
update script as below; after // zip_file.delete();
var isDeleted = this.Files.deleteIfExists(zip_file.toPath());
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 05:30 AM
Thanks for your response.
added code but files are not deleted , Please help me .
packages also added.
var isDeleted = this.Files.deleteIfExists(zip_file.toPath());
Thanks
Ram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 06:23 AM
Hi,
I had used somewhat similar to this previously and it worked well
what does this contain?
zip_file ->
Did you verify if the zip file itself is present/created
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 06:42 AM
Thanks for your response.
Inside folder Zip _files is there, so we need to delete that Zip _Files .
Thanks
Ram