【问题标题】:How to Upload and Display File Using Play Framework and jQuery?如何使用 Play Framework 和 jQuery 上传和显示文件?
【发布时间】:2015-10-14 05:49:27
【问题描述】:

我有一种情况:我正在使用 https://www.playframework.com/documentation/2.0/JavaFileUpload 在某个服务器位置上传一个文件,

//上传文件:

<input type="file" name="fileUpload">
<input type="submit" value="Upload">

从下面的代码中,我正在上传上面上传的文件并在我的视图页面上获取/显示它(点击提交按钮后):

<input type="file" id="inputfile">
<input type="button" value="Submit" id="submitfile">

jQuery:

$("#submitfile").click(function(){
    var path1 =$('input[type=file]').val().replace(/C:\\fakepath\\/i, '');//uploaded file names 
    //adding the Play framework server path for Application to get the image(s) file(s) path
    var filespath = '/files/images/'+path1;//giving my uploaded files path here

    });

但我的要求是:我只需要一种类型:在服务器位置接受/上传文件并在我的视图页面上从服务器位置返回/显示相同的文件路径?我正在为此而奋斗。请帮帮我。

【问题讨论】:

    标签: javascript jquery html playframework


    【解决方案1】:

    这看起来与33163555 类似。该问题有以下示例:

    编辑: 参考 2320069 以获得对 ajax 文件上传和替代方案的支持:

    FormData 支持从以下桌面浏览器版本开始。 IE 10+、Firefox 4.0+、Chrome 7+、Safari 5+、Opera 12+

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    
    <form enctype="multipart/form-data">
      <input type="file" id="file" name="file" />
      <input type="submit" id="submit" name="" value="Upload" />
    </form>
    
    <script>
    $('#submit').click(function (event) {
       event.preventDefault();
       var file = $('#file').get(0).files[0];
       var formData = new FormData();
       formData.append('file', file);
       $.ajax({
           url: 'upload',
           data: formData,
           type: 'POST',
           contentType: false,
           processData: false,
           beforeSend: function (data) {
             alert('Are you sure you want to upload document?');
           },
           success: function (data) {
             //call your jQuery action here
             alert('Upload completed: ' + data);
           },
           error: function (jqXHR, textStatus, errorThrown) {
             alert(textStatus + ': ' + errorThrown);
           }
        });
        return false;
    });
    </script>
    

    在您的路线中,您有:

    POST /upload controllers.Application.upload()
    

    您的控制器方法返回文件路径的位置:

    public static Result upload() {
      MultipartFormData body = request().body().asMultipartFormData();
      FilePart fileP = body.getFile("file");
      if (fileP != null) {
        File file = fileP.getFile();
        //If we want to move from temp
        //FileUtils.moveFile(file.getCanonicalPath(), "FileB"); 
        return ok(file.getCanonicalPath());
      } else {
        return badRequest("Upload Error");    
      }
    }
    

    您可以在 ajax 成功回调中执行自定义 jQuery 操作

    【讨论】:

    • 我收到内部服务器错误
    • 我们可以像上面那样实现它吗:stackoverflow.com/questions/33209460/…
    • 哦对不起哈哈。我没有意识到您发布了我引用的问题。根据2320069204528536974684 编辑我的答案,这表示您需要在发送之前将文件存储到 ajax formData 对象中,或者采取其他方法
    • 太好了,效果很好!!脱帽了!!你救了我!!
    • 我收到上述 ajax 调用的内部服务器错误
    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 2011-06-08
    • 2015-10-11
    • 2013-01-10
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多