2

i want to upload an image file along with some data through a struts form, process data , and store that image in DB

i have created a form for it , defined action for uploading file in struts.xml file , but when the control comes in java method ,getting null in the File type variable, please help on it.

code in struts.xml :

    <action name="uploadFile" class="org.ui.LogActivityAction"
        method="uploadFile">
        <interceptor-ref name="fileUpload">  
            <param name="fileUpload.maximumSize">10485760</param>
            <param name="fileUpload.allowedTypes">text/plain,image/jpg</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"/>
        <result name="success">/user/activity/upload-card.jsp</result>
        <result name="error">/user/activity/upload-card.jsp</result>
    </action>

ajax used for sending data to action :

 function saveSalesUserActivity(card) {
     var isValidForm = jQuery('#dailyActivity').valid();
     isValidForm = jQuery('#dailyActivity').valid();
     if (!isValidForm) {
         return;
     }
     if (isValidForm) {
         jQuery.blockUI({
             message: "<strong>" + messages("mis.common.wait") + "</strong>"
         });
         jQuery.ajax({
             type: "POST",
             url: "/sales/user/logactivity/saveactivity.action?businessCard=" + card,
             data: jQuery('#dailyActivity').serialize(),
             success: function(response) {
                 jQuery("#miscontent").html(response);
             },
             error: function(response) {
                 jQuery("#miscontent").html(response);
             }
         });
         jQuery.unblockUI();
     }
     jQuery.unblockUI();
 }

through this when reaching in java method,I am not getting the value of File type variable( used 'businessCard' here).. anyone help..

Vikas Taank
  • 101
  • 1
  • 12

2 Answers2

1

If you used jQuery('#dailyActivity').serialize(),
It it is not working for <input type'file'>
Have look at this jsFiddle Does not works

and this one .serialize()

Data from file select elements is not serialized.

To send <input type'file'> you may want to try this

var formData = new FormData($('form')[0]);

Have look at this https://stackoverflow.com/a/8758614/3425489

Community
  • 1
  • 1
Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42
0

For uploading file using jQuery you have to use formdata

Working code for file uploading is below

$(document).on('click', '#upload', function(e) {
          e.preventDefault();
      var fd = new FormData();
      var file = $('#my_file')[0].files[0];
      fd.append('file', file);
      fd.append('userId', $('#userid').val());
      console.log("hi");
      $.ajax({
          url: 'UploadPic',
          data: fd,
          type: "POST",
          contentType: false,
          processData: false,
          success: function(dd) {
            alert("sucessfully Uploaded")
            }
          });
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="my_file">
<input type="hidden" id="userid" />
<input type="button" id="upload" value="Upload" />
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xrcwrn
  • 5,339
  • 17
  • 68
  • 129