WEB 檔案上傳

來源:互聯網
上載者:User

標籤:ram   ash   bsp   移動   util   output   請求   點擊   dictd   

一、背景檔案上傳action

    private String tempPath = this.servletContext.getRealPath("") + File.separator + "tempFolder";    private String realPath = this.servletContext.getRealPath("") + File.separator + "realFolder";    private File attach;//前台傳入    private String attachContentType;    private String attachFileName;    private String attachTempName;    /**     * 建立任務時附件首先上傳到臨時檔案夾     * @return     */    @Action(value="UploadTemp")    public String UploadTemp(){                System.out.println("!!!!!!!進入檔案上傳!!!!!!!");        int fileState = 0;//標識目標檔案是否存在,如果存在提醒修改檔案名稱 0表示不重複,1表示重複,2表示檔案複製出錯        String wpyId = request.getParameter("wpyId");        //臨時檔案由原檔案名稱+#+使用者名稱組成以防重複,個人每次任務只傳一個檔案,正式傳完即刪除臨時檔案夾內容,不會重複        String attachTempName = wpyId+"#"+attachFileName;        //看臨時路徑是否存在        Util.checkDirExist(tempPath);        String attachFile = tempPath + File.separator + attachTempName;        //建立目標檔案        File destFile = new File(attachFile);          if (destFile.exists()) {            new File(attachFile).delete();          }                int byteread = 0; // 讀取的位元組數        InputStream in = null;          OutputStream out = null;          try {              in = new FileInputStream(attach);              out = new FileOutputStream(destFile);              byte[] buffer = new byte[1024];              //定義一個流 in,存入buffer中,=-1代表讀完            while ((byteread = in.read(buffer)) != -1) {                  out.write(buffer, 0, byteread);              }  //                return true;          } catch (FileNotFoundException e) {              fileState = 2;            e.printStackTrace();  //                return false;          } catch (IOException e) {              fileState = 2;            e.printStackTrace();  //                return false;          } finally {              try {                  if (out != null)                      out.close();                  if (in != null)                      in.close();                 attach.delete();            } catch (IOException e) {                  e.printStackTrace();              }          }                Map<String, Object> fileInfo = new HashMap<String, Object>();        fileInfo.put("uploadName", attachTempName);        fileInfo.put("fileState", fileState);        outputJson(fileInfo);        return NONE;    }

二、前台頁面

<div class="col-sm-2" style="padding-right:0px"><a id="advancedDropzone" class="btn btn-primary" style="width:100%">點擊上傳圖片</a></div><div class="col-sm-6" style="padding-left:0px"><div class="input-group">          <input readonly type="text" id="img_name" class="form-control">            <div class="input-group-btn">               <a id="delTempFile" tabindex="-1" class="btn btn-primary" type="button">刪除檔案</a>            </div>        </div></div>

 三、js 發起請求

 

//上傳附件按鈕組件        var example_dropzone = $("#advancedDropzone").dropzone({            //首先上傳到臨時位置,如果整體提交,再移動到規定位置,如果整體取消,則刪除臨時檔案            url: baseCtx+‘/.../UploadTemp.action?Id=‘+curuser,            paramName: "attach",             acceptedFiles: ".jpg,.png,.bmp",            dictDefaultMessage:"點擊上傳檔案",            maxFiles:1,//一次性上傳的檔案數量上限            maxFilesize: 20, //MB            addedfile: function(file)            {                var size = parseInt(file.size/1024, 10);                size = file.size < 1024 ? (file.size + " 位元組") : (size + " KB");                upLoadSize = size;                $("#img_name").val(file.name+"    "+upLoadSize+"    上傳中...");            },            success:function(file,data){                var currData = $.parseJSON(data);                uploadName = currData.uploadName;                $("#opr_screenshot_name").val(uploadName+"    "+upLoadSize+"    已完成");                //啟用刪除臨時檔案按鈕                $("#delTempFile").off().on("click",function(){                    $("#opr_screenshot_name").val("");                    $.ajax({                        url : baseCtx+‘/.../delTempFile.action‘, //TODO                        data : {                            attachTempName:uploadName                        },                        type : ‘post‘,                        async : false,                        dataType : "json",                        success: function(data){                                                    }                    })                })            },            error: function(file)            {                alert(file.name+"上傳未成功")            },            removedfile:function(file){                uploadName="";            }        })     })

 

WEB 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.