How to upload Microsoft word .docx and .xlsx files using Struts2 - struts2

We are using Struts2 in our application and I am using the struts2 upload functionality for file uploading. Now my requirement is, we need to let the user upload to ".docx" and ".xlsx". I listed "application/msword" and "application/vnd.ms-excel" as allowedTypes in strut.xml. By this we are able to upload only ".doc" and ".xls" files, but not ".docx" and ".xlsx" files. Is there any workaround for this?

From http://sanjaal.com/java/tag/microsoft-office-2010-mime-types/
The following are the mime types to be used for MS Office 2010 document file formats.
.docm: application/vnd.ms-word.document.macroEnabled.12
.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotm: application/vnd.ms-word.template.macroEnabled.12
.dotx: application/vnd.openxmlformats-officedocument.wordprocessingml.template
.potm: application/vnd.ms-powerpoint.template.macroEnabled.12
.potx: application/vnd.openxmlformats-officedocument.presentationml.template
.ppam: application/vnd.ms-powerpoint.addin.macroEnabled.12
.ppsm: application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.ppsx: application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
.xlam: application/vnd.ms-excel.addin.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsm: application/vnd.ms-excel.sheet.macroEnabled.12
.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltm: application/vnd.ms-excel.template.macroEnabled.12
.xltx: application/vnd.openxmlformats-officedocument.spreadsheetml
.xps: application/vnd.ms-xpsdocument

You could do it by validating the file name in the actions that handle the uploaded files. For example:
private File attachement;
...
public void validate() {
if (attachement.getName().endsWith(".docx") || (attachement.getName().endsWith(".xlsx")))
addActionError(...);
}

Related

When I download Excel files, strange files are stored

I want to download uploaded files such as .zip, .xlsm, xlsx, pdf... ..., etc., that have been uploaded, I want to eventually compress them into a zip file for download.
However, if the uploaded files are not zipped, they will be downloaded with strange files stored in them.
In this case, .xlsm
Source Code
class DownloadZipsController < ::ApplicationController
def index
file_name = "#{Time.current.strftime("%Y%m%d_%H%M%S")}.zip"
zip_file = Tempfile.new(file_name)
Zip.unicode_names = true
Zip::OutputStream.open(zip_file.path) do |zip|
params[:product_ids].each do |product_id|
product = Product.find(product_id)
zip.put_next_entry("output.zip")
zip.print Net::HTTP.get URI.parse(product.submit_zip_file.url)
end
end
send_file zip_file.path,
type: "application/zip",
disposition: "attachment",
filename: file_name
zip_file.close
rescue => e
logger.debug e.backtrace
redirect_to request.referer, alert: e.message
end
end
Uploaded files are stored in AWS S3.
Is there any solution to this problem?
Office Documents such as .xlsm, .xlsx, .docx, and others are in fact zip files containing the document as a xml file plus additional resources.
The file tree you have shown in your screenshot shows the content of one such document if you interpret it as a zip file and unpack.
It appears that somewhere in your code, you have detected the document file as a zip file and interpreted it as such which resulted in its contents to be unpacked.
This is not apparent from the code you have posted though, so I would assume that you have some additional handling of zip files somewhere else (such as a function to download existing files which may then be send with a wrong content type to the browser, i.e as an application/zip rather than application/vnd.ms-excel.sheet.macroEnabled.12).

Download a zip file from eBay using rest-client

I want to download the zip file from eBay. Using downloadfile api.
response = RestClient.post(url,xml,headers)
This call return the content of zip file that is not exractable in xml I think. So i want to download zip file as it is from eBay.
My code is:
headers = {
"X-EBAY-SOA-OPERATION-NAME"=>"downloadFile",
"X-EBAY-SOA-SECURITY-TOKEN" => access_token_lister,
"X-EBAY-API-SITEID"=>"0",
"Content-Type"=>"application/zip"
}
url = 'https://storage.ebay.com/FileTransferService'
xml = '<?xml version="1.0"
encoding="utf-8"?> <downloadFileRequest xmlns="ebay.com/marketplace/services">; <fileReferenceId>6637191637</fileReferenceId> <taskReferenceId>6474385857</taskReferenceId> </downloadFileRequest>'
The documentation for the API used above can be found here : http://developer.ebay.com/devzone/file-transfer/CallRef/downloadFile.html
You will have to pass the content of the response to a library like rubyzip. Then use it to extract your files from the zipped file. This you can do by first writing the file to disk, and then reading it using rubyzip.
Specifically look at this part of the documentation of rubyzip - Reading a Zip file to accomplish what you are trying to achieve.

Error while uploading .xlsx file using ExcelDataReader

I am working on asp.net MVC project.
I use ExcelDataReader component to read excel file records.
Now when I published my project to server and upload a .xlsx file with uploader I get below mentioned exception message. There are no errors with local deployment but server.
Access to the path '\Microsoft Corporation\Internet Information
Services\7.5.7600.16385' is denied.
and code where I am getting error is:
if (personsFile.FileExtension == ".xls")
{
Stream st = new MemoryStream(personsFile.FileArray);
reader = ExcelReaderFactory.CreateBinaryReader(st);
}
else if (personsFile.FileExtension == ".xlsx")
{
Stream st = new MemoryStream(personsFile.FileArray);
//exception occured on under line
reader = ExcelReaderFactory.CreateOpenXmlReader(st);
}
But when I upload a .xls file, I dont have any error.
How to resolve issue with .xlsx extenstion?
This is most likely due to ExcelDataReader 2.x extracting the xlsx archive to %TEMP% before processing it. The current pre-alpha of 3.0 no longer does this. See https://github.com/ExcelDataReader/ExcelDataReader/releases.

FTP getbinaryfile by file extension?

I am using Net::FTP's getbinaryfile functionality to pull in a zip file with FTP. My system is not aware of the full file name, so I would simply like to search the folder for the zip file extension. Usually I would simply input the filename as *.zip. This does not seem to work.
ftp = Net::FTP.new(domain)
path = "#{Rails.root}/public/ftp/#{self.id}.zip"
ftp.getbinaryfile("*.zip", path)
I used the following code to return the zip file name in the FTP folder. Then using the same code as above, I was able to run getbinaryfile with the correct zip file name.
files = ftp.nlst("*.zip")
I use the following to get all zip files (I'm using SFTP but hopefully this will point you in the right direction)
Net::SFTP.start(domain, user, :password => 'pass') do |sftp|
sftp.dir.glob("/yourdirectory","*.zip").each do |file|
sftp.download!(file, "/local/spot")
end
end

Checking contents of Uploaded file

I am using ASP.NEt MVC . I want to upload .zip files for which I am using html input file upload control on my view.
I want only .zip files to be uploaded.
I want to check that my .zip contains only two files - both having extensions .txt and one of them having name "start".
Can anyone please suggest me about how to check this? How can we assure that the uploaded .zip is really a zipped folder and not any other file having just .zip extension.
can we use HttpPostedFileBase.ContentType?
thanks in advance,
kaps
The ContentType is simply filled in by the client browser, so you can't really trust it. The only way is to try & parse the file on the server: use something like SharpZipLib to open the .zip file and confirm that it has two files in it.
You can use DotNetZip to determine whether file is a zip file or not. And you can do much more with dotnetzip.
You can verify like this
[HttpPost]
public ActionResult Index(FormItems item, HttpPostedFileBase files)
{
//check for zip file
try
{
ZipFile.CheckZip(files.FileName); //just pass the name of the file
}
catch
{
//not a zip file
}
}
OR
[HttpPost]
public ActionResult Index(FormItems item, HttpPostedFileBase files)
{
//check for zip file
try
{
ZipFile.Read(files.InputStream); //read the zip contents by passing the input stream
}
catch
{
//not a zip file
}
}
Make sure that you include namespace using Ionic.Zip; and add a reference to that dll as well.
Hope this helps

Resources