weixin_33713503 2018-05-03 09:19 采纳率: 0%
浏览 30

上传照片Spring-Ajax

I am trying to upload a photo and get a preview of the uploaded image using Spring and Ajax.

I have the following code:

<h3>File Upload</h3>
    <ul>
        <li>
            <form id="upload-form" method="post" enctype="multipart/form-data">
                Select a file: <input type="file" name="uploadfile" size="45" accept="*" />
                <br>
                <input id="submit-button" type="submit" value="Upload" />
            </form>
        </li>
        <li><p>Result: <br><span id="result"></span></p></li>
    </ul>

    <h3>Show Image</h3>
    <ui>
        <li>original:<img id="image-o" src="#" alt="original image" /></li>
        <li>small: <img id="image-s" src="#" alt="small image" /></li>
        <li>medium: <img id="image-m" src="#" alt="medium image" /></li>
        <li>large: <img id="image-l" src="#" alt="large image" /></li>
        <li>extra large: <img id="image-xl" src="#" alt="extra large image" /></li>
    </ui>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#submit-button").on("click", function (event) {
                event.preventDefault();

                // create an FormData object
                var data = new FormData($('#upload-form')[0]);

                // disabled the submit button
                $("#submit-button").prop("disabled", true);

                // post data
                $.ajax({
                    type: "POST",
                    enctype: 'multipart/form-data',
                    url: "image/api/upload",
                    data: data,
                    processData: false,
                    contentType: false,
                    cache: false,
                    timeout: 600000,

                    success: function (data) {
                        // shows server's response
                      //  $("#result").text(data);
                        console.log("SUCCESS: ", data);

                        enableSubmitButton();
                        updateImages(data);
                    },

                    error: function (e) {
                        // shows server's response
                      //  $("#result").text(e.responseText);
                        console.log("ERROR: ", e);

                        enableSubmitButton();
                        updateImages(e.responseText);
                    }
                });
            });
        });

        function enableSubmitButton() {
            $("#submit-button").prop("disabled", false);
        }

        function updateImages(data) {
            var url = 'http://localhost:9001/image/api/' + data;
            $('#image-s').attr('src',url + '?size=s');
            $('#image-m').attr('src',url + '?size=m');
            $('#image-l').attr('src',url + '?size=l');
            $('#image-xl').attr('src',url + '?size=xl');
            $('#image-o').attr('src',url + '?size=o');
        }
    </script>

And my Java code:

@POST
@Path("/upload")
@Consumes(ExtendedMediaType.MULTIPART_FORM_DATA)
@Produces(ExtendedMediaType.APPLICATION_JSON_UTF8)
@Transactional
public ResponseEntity<Void> uploadImage(@RequestParam("uploadfile") MultipartFile file) {

if (file.getSize() < maxFileSize && validExtensions.contains(file.getContentType())) {

        Image image = Image.builder().id(file.getSize()).build();
        imageServiceConfigMapper.saveImage(image);

       /* FormDataContentDisposition fileDetail = null;
        ImageMetadata imageMetadata = buildImageMetadata(fileDetail, image);
        imageServiceConfigMapper.saveMetadata(imageMetadata);*/
    }
        return new ResponseEntity<>(HttpStatus.OK);

When I choose a photo from my PC, it is accepted - see screenshot below:

enter image description here

When I click in upload, my browser gives the following answer:

enter image description here

The JSON looks like this:

enter image description here

BUT the selected picture is not showing:

enter image description here

Am I using a wrong URL? The address of the site where I got the above screen ends is the one with index.html at the end, but I defined /api/upload as a relative path... If I open the relative path, I got the following:

enter image description here

Or is it something wrong with the code responsible for the preview?

Sorry, I know there is a tones of similar issues but I could not found anything that hepled. I am quite a beginner... Sorry for the long post and thanks for the help in advance!

  • 写回答

1条回答 默认 最新

  • weixin_33691700 2018-05-03 22:07
    关注

    Spring Content can do this and has a getting started guide and git repo with an angularjs based front-end that demonstrates exactly what you are trying to do. The getting started guide is here.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大