NutzCN Logo
问答 ajax使用 FormData上传文件,只有文件拿不到。。。。
发布于 2455天前 作者 wcc1433 1162 次浏览 复制 上一个帖子 下一个帖子
标签:

使用 H5的 FormData对象,提交表单数据,包含文件,其它的数据都能拿到,只有文件对象拿不到

前台代码

var form = new FormData();//创建表单对象
        form.append("communityParenty.name", name);
        form.append("communityParenty.phone", phone);
        form.append("communityParenty.startDate", startDate);
        form.append("communityParenty.overDate", endDate);
        form.append("communityParenty.status", status);
        form.append("communityParenty.description", discription);
        if(headImg != null) {
            form.append("headImg.fileName", headImg.name);
            form.append("headImg.fileSize", headImg.size);
            form.append("headImg.fileType", headImg.type);
            form.append("headImg.file", headImg);
        }

        var url = "/communityParenty/addCommunityParenty";
        $.ajax({
            type: "POST",
            url: url,
            enctype: "multipart/form-data",
            contentType: false,
            processData: false,
            cache: false,
            data: form,
            dataType: "json",
            xhr: xhrOnProgress(function(e) {
                var percent=e.loaded / e.total;//计算百分比
                console.log("ajax发送数据百分比:" + percent);
            }),
            success: function(jsonData) {
                var data = $.parseJSON(jsonData);
                alert(jsonData);
            },
            error: function() {
                alert("请求失败");
            }
        });
    }

后台代码

@Ok("json")
    @AdaptBy(type = UploadAdaptor.class)
    public Object addCommunityParenty(HttpServletRequest request, HttpServletResponse response,
                                      @Param("::communityParenty.") UserCommunityParenty userCommunityParenty,
                                      @Param("::headImg.") FileEntity headImg) throws IOException, ServletException {
        //FileEntity是一个 文件对象,里面包含了一个 File对象
}
8 回复

ajaxfileupload 才可以吧

我用 VoidAdaptor获取流尝试了,可以拿到文件,但是用 UploadAdaptor就不行了

贴代码看看?

@At("/addCommunityParenty")
    @Ok("json")
    @AdaptBy(type = VoidAdaptor.class)
    public Object addCommunityParenty(HttpServletRequest request, HttpServletResponse response,
                                      @Param("::communityParenty.") UserCommunityParenty userCommunityParenty,
                                      @Param("::headImg.") FileEntity headImg) throws IOException, ServletException {
        ServletInputStream sis = request.getInputStream();        //获取整个request输入流
        byte[] buf = new byte[1024];
        int flag = 0;        //记录每次读取的字节个数
        while ((flag = sis.readLine(buf, 0, 1024)) != -1)        //每次读入一行,存入buf中
        {
            String line = new String(buf, 0, flag);
            System.out.println(line);
        }
        sis.close();
    }

获取的数据

image/jpeg

------WebKitFormBoundaryVR5zO1wGopxLew4q

Content-Disposition: form-data; name="headImg.file"; filename="营业执照1.jpg"

Content-Type: image/jpeg



���� Exif  II*           �� Ducky     <  ��rhttp://ns.adobe.com/xap/1.0/ <?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27        "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmpMM:OriginalDocumentID="xmp.did:e645d1bb-4e75-5c4e-b79c-0a0d45551709" xmpMM:DocumentID="xmp.did:5C092CD4F7D611E68CF4E201095F89C7" xmpMM:InstanceID="xmp.iid:5C092CD3F7D611E68CF4E201095F89C7" xmp:CreatorTool="Adobe Photoshop CC (Windows)"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:C5C79953F7D511E6A8F1A89604928FE4" stRef:documentID="xmp.did:C5C79954F7D511E6A8F1A89604928FE4"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>�� Adobe d�   �� � 		







�� �� �� �                        !1AQ"aq2����B#�Rb3$���C�r�4%S�D5&��c��d�E����e6FG�sU�'f   !1AQ�aq������"�2BR��b��r��#�£Cc��3S�4��   ? �Kp��(�:^�0'J��T�(?

S!���:-L�pʍA���M1=��jE��4r6� :�@��[�δ� ���ǢPP�㝾�~�*!�d�\}t��G#J��4�a�(��TP.0D���q���U3�K�m�006=�� �

@.�6����ww��~F���� ��jd-��d	�ҋ����V��"��� H��}��x� �E�xԨ�o� ?�a(!.9�q���ЀGq���?�eD����������}j���i�i �(=��ֈ�����Z�m�����

�E�_��"��d_�f��� �ă���)(�?w����	�h 7� ]�|/Bk���ƒ��"��[cSq�j ��B�jlZ�(	����B��4�Rh&Q*��[J���U$-���H[�¡���*�0.��T�0T��s��Z�e u���

6[��=n�P!	 �㨤�֕US�-I6%7}�A�f���@Z�w�=) Aq ��C��v��TN��@%�(!�:�����ݫ�T^�����hb�ީ 5/s�Ni�ר����M�Ґ

p���(k �HP(K֠K@)���F��\
省略省略--------------------------------------------------

试试

@Param("headImg.file") TempFile tmp

OK,没有问题了 感谢,在 nutz里面,文件上传都是用 TempFile吗??

添加回复
请先登陆
回到顶部