在struts中上传文件以及一些数据 [英] upload a file along with some data in struts

查看:74
本文介绍了在struts中上传文件以及一些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过struts表单上载图像文件以及一些数据,处理数据,并将该图像存储在DB中

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

我为此创建了一个表单,定义了用于在struts.xml文件中上传文件的动作,但是当控件进入java方法时,在File type变量中获取null时,请帮忙.

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.

struts.xml中的代码:

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用于将数据发送到操作:

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();
 }

通过这种方法到达java方法时,我没有得到File类型变量的值(在这里使用'businessCard'). 任何人的帮助.

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

推荐答案

如果使用jQuery('#dailyActivity').serialize()
它不适用于<input type'file'>
看看这个 jsFiddle 不起作用

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

和这一个 .serialize()

文件选择元素中的数据未序列化.

Data from file select elements is not serialized.

要发送<input type'file'>,您可能需要尝试此操作

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

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

请查看此 https://stackoverflow.com/a/8758614/3425489

这篇关于在struts中上传文件以及一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆