<input type=“file“>上传文件到服务器

问题:

如何在浏览器端上传文件(单个文件/文件夹)到服务器?

浏览器:firefox

服务器:虚拟机ubuntu18

1.前端

 <li><a href="#"  onclick='openFileDialog()'>选择图片路径</a></li>
 <li><input type="file" id="folder2images" style="display: none"  onchange='fileSelected()'webkitdirectory/></li>

补充:如只想上传单个文件,就把webkitdirectory去掉

2.js

function openFileDialog()

{
    $("#folder2images").click();

}
function fileSelected(){
    var file=document.getElementById("folder2images").files;
    console.log(file)
    startFileUpload(file);

}

//开始上传

function startFileUpload(file)

{

    var uploadURL = "FilesUploadServer";

//手工构造一个form对象

    var formData = new FormData();

    for(var i=0;i<file.length;i++){
        formData.append("file" , file[i]);// 'file' 为HTTP Post里的字段名, file 对浏览器里的File对象;注意:此处如果上传的是文件夹,不能直接formData.append("file" , file);这样后台会接收不到文件
    }

//手工构造一个请求对象,用这个对象发送表单数据

//设置 progress, load, error, abort 4个事件处理器

    var request = new XMLHttpRequest();

    request.upload.addEventListener("progress" , window.evt_upload_progress , false);

    request.addEventListener("load", window.evt_upload_complete, false);

    request.addEventListener("error", window.evt_upload_failed, false);

    request.addEventListener("abort", window.evt_upload_cancel, false);

    request.open("POST", uploadURL ); // 设置服务URL
    request.send(formData); // 发送表单数据

}

window.evt_upload_progress = function(evt)

{

    if(evt.lengthComputable)

    {

        var progress = Math.round(evt.loaded * 100 / evt.total);

        console.log("上传进度" + progress);

    }

};

window.evt_upload_complete = function (evt)

{

    if(evt.loaded == 0)

    {

        console.log ("上传失败!");

    }

    else

    {

        console.log ("上传完成!");
        //var response = JSON.parse(evt.target.responseText);
        var response=evt.target.responseText;
        console.log ("response:  "+response);

    }

};

window.evt_upload_failed = function (evt)

{

    console.log ("上传出错");

};

window.evt_upload_cancel = function (evt)

{

    console.log( "上传中止!");

};

3.后端

@RequestMapping(value="/FilesUploadServer")
    public @ResponseBody String FilesUploadServer(@RequestParam(value = "file")MultipartFile[] files){//前后端参数传递时,名称不一样可以用@RequestParam
        String fileName = null;
        String msg = "";
        if (files != null && files.length >0) {
            for(int i =0 ;i< files.length; i++){
                try {
                    fileName = files[i].getOriginalFilename();
                    byte[] bytes = files[i].getBytes();
                    File file = new File("**自己的路径**"+"/" + fileName);
                    File fileParent = file.getParentFile();
                    if(!fileParent.exists()){
                        fileParent.mkdirs();
                    }
                    file.createNewFile();
                    BufferedOutputStream buffStream =
                            new BufferedOutputStream(new FileOutputStream(new File("**自己的路径**"+"/" + fileName)));
                    buffStream.write(bytes);
                    buffStream.close();
                    msg += "You have successfully uploaded " + fileName;
                } catch (Exception e) {
                    return "You failed to upload " + fileName + ": " + e.getMessage();
                }
            }
            return msg;
        } else {
            return "Unable to upload. Files is empty.";
        }
    }

4.结果

以上传文件3.txt为例:

 

至此上传文件成功!

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<tr> <th contenteditable="true" ><input type="text" oninput="filterTable(1)" placeholder="aoiStep" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(2)" placeholder="defectType" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(3)" placeholder="layerCode" ></th> <th contenteditable="true" ><input type="text" oninput="filterTable(4)" placeholder="type"></th> <th contenteditable="true" ><input type="text" oninput="filterTable(5)" placeholder="dpet" ></th> <th contenteditable="true"><input type="text" oninput="filterTable(6)" placeholder="subcode" ></th> <th contenteditable="true" ><input type="text" placeholder="codeDescription" ></th> <th contenteditable="true" >image1</th> <th contenteditable="true" >image2</th> <th contenteditable="true">image3</th> <th contenteditable="true" >image4</th> <th contenteditable="true" >image5</th> <th contenteditable="true" ><input type="text" placeholder="determination_rule"></th> </tr> </thead> <tbody> <form action="upload.php" method="POST" enctype="multipart/form-data"> <tr> <td input type="text" name="aoi_step">3</td> <td input type="text" name="defect_type">Particle</td> <td input type="text" name="layer_code">ACT</td> <td input type="text" name="type">Particle</td> <td input type="text" name="dpet">ACT</td> <td input type="text" name="subcode">Particle</td> <td input type="text" name="code_description">ACT</td> <td> <input type="file" name="image1_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> <td> <input type="file" name="image2_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image3_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image4_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" name="image5_path" onchange="previewImage(this);selectCell(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td input type="text" name="determination_rule">Particle</td> <table id="data_table"> <thead> </form> </tbody> </table> 我只需要上传表格内容,不需要上传表头内容,应该怎么处理
最新发布
07-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值