0

I have problem when I'm uploading a csv file by web page jsp. I can upload many files with my Struts2 application. I'm using Struts 2.5.20 and I can upload all Mime types in "allowedTypes" except "text/csv".

    <action name="uploadDocuments" class="UploadDocumentsAction" method="uploadDocuments">
        
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">5242880</param>
            <param name="allowedTypes">image/jpeg,
                image/jpeg2000,
                image/png,
                image/gif,
                image/bmp,
                image/tiff,
                image/apng,
                text/plain,
                text/csv,
                application/pdf,
                application/msword, 
                application/vnd.openxmlformats-officedocument.wordprocessingml.document</param>
        </interceptor-ref> 
        <interceptor-ref name="defaultStack"></interceptor-ref>
          
        <result name="success" type="stream">
            <param name="contentType">text/html</param>  
            <param name="inputName">stream</param>                     
        </result>
    </action>

With Jquery Ajax I invoke action and if "success" show "File Upload Success", otherwise alert "check the extensions of the allowed file types..."

When I try to upload a Csv file I have alert message. I don't understand because only csv File doesn't work, all other files in "allowedTypes" work fine.

I put breakonpoint in my Action, If I try csv file doesn't pass in Java code, when I select other format files it passes.

Thanks

  • Used wrong mine type? – Roman C Jul 18 '20 at 06:45
  • I used also application/csv but it didn't work. Thanks – user1361416 Jul 19 '20 at 08:57
  • You have used the MIME type to allow [fileupload interceptor](https://stackoverflow.com/a/25574995/573032) to upload a file but specific error is not concerned with it. To define the problem you need to submit more details relevant to your question. – Roman C Jul 20 '20 at 00:49
  • I added application/csv,application/x-csv,text/csv, text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values, I cannot upload only files with extension .csv , I put breakonpoin in MyAction but when I select a csv doesn't pass in the java Code , I can debug only javascript Browser and I can see "404 (Not Found)" with "uploadDocuments.do" row "$.ajax" of my file js. I have not errors in Java Code, none Exception – user1361416 Jul 20 '20 at 12:33
  • Inspect with Browser I see ErrorNo result defined for action myPackage.UploadDocumentsAction and result input – user1361416 Jul 20 '20 at 14:24
  • If you can't reach the code of your action method then something wrong is happens before your action should invoke. To learn more about error see [this](https://stackoverflow.com/a/15119438/573032) answer. – Roman C Jul 20 '20 at 22:08
  • Hi Roman, thanks, I'm not using Validation Interceptor, If I move order of interceptor in action tag, defaultStack before fileUpload it works csv upload, but I can upload all type of files. "allowedTypes" is not working then – user1361416 Jul 21 '20 at 08:31
  • Solved, maybe. I try Mkyong 's Example https://mkyong.com/struts2/struts-2-file-upload-example, I found content-type output of a csv file is "application/vnd.ms-excel" . But I don't want this mime type because it inclused files xls, xlt, xla... I want to upload only images, files pdf or text . Thanks – user1361416 Jul 24 '20 at 13:41
  • I'm doubt that you have implemented any validation logic in your file uploading application. Hense you have wrong configuration of interseptors. – Roman C Jul 25 '20 at 18:44
  • I try Mkyong's Example with struts 2.5.20 and also it doesnt't work with csv files. Content type printed in page is application/vnd.ms-excel. My web application has correct interceptors. Thanks – user1361416 Jul 26 '20 at 20:39
  • This example should work with the version of Struts2 used at the time this example was written. Because you have not posted all the code required to reproduce the error it's impossible to guess a solution for this problem. Other tries is to correct the parameters of the request like in [this](https://stackoverflow.com/a/16124909/573032) answer. – Roman C Jul 27 '20 at 05:13
  • Hi Roman, I think it's a Strut2 bug. I try other Example https://www.journaldev.com/2192/struts-2-file-upload-example, with correct libraries, struts 2.5.20 common-fileUpload 1.4, commons-io 2.6. I have the same error Content-Type not allowed: file "SampleCSVFile_2kb.csv" "upload_2bd1107f_d6ad_4332_819f_91353f1b5e9e_00000002.tmp" application/vnd.ms-excel , when I select a Csv File – user1361416 Jul 27 '20 at 08:53
  • /UploadFile.jsp myfiles /UploadFileSuccess.jsp /UploadFile.jsp – user1361416 Jul 27 '20 at 08:55
  • 10485760 text/plain,image/jpeg,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf, – user1361416 Jul 27 '20 at 08:55
  • /UploadMultipleFile.jsp myfiles /UploadMultipleFileSuccess.jsp /UploadMultipleFile.jsp 10485760 text/plain,image/jpeg – user1361416 Jul 27 '20 at 08:55

0 Answers0