在jsp中自动上传没有页面刷新和jquery的图像

时间:2015-05-02 07:03:55

标签: java ajax jsp image-uploading

function showMyImage(fileInput){ 
    alert('inside showMyImage');
    var files = fileInput.files;
    for (var i = 0; i < files.length; i++) {           
        var file = files[i];
        var imageType = /image.*/;     
        if (!file.type.match(imageType)) {
            continue;
        }           
        var img = document.getElementById("output");

        img.file = file;    
        var reader = new FileReader();
        reader.onload = (function(aImg) { 
            return function(e) { 
                aImg.src = e.target.result;
                //alert(aImg.src);
            }; 
        })(img);
        reader.readAsDataURL(file);
    }

    var formData = new FormData();
    formData.append( 'file', $('#artistprofileuploadfileField')[0].files[0]);
   // formData.append('image', $('input[type=file]')[0].files[0]);
    alert(formData);
    $.ajax({
        url: "/uploadImage",
        data:formData,
        type: "POST",
        contentType: false,//'multipart/form-data',
        processData: false,
        cache: false,
        success: function() {
            alert("success");
            document.getElementById("imgDiv").innerHTML=xmlhttp.responseText;

        },
        error: function() {
            alert("unable to create the record");
        }
    });

     //this.myform.submit();
}

我在jsp上自动上传图片时遇到问题。我想上传它并使用ajax将其保存到一个文件夹,因为我不想刷新整个页面,但我不喜欢使用jQuery的ajax。我有一个带有enc type =&#34; multipart / form-data&#34;的表单,其中有很多复选框,文本框,下拉列表和图像标签。 如果我将表单的其他详细信息保存到数据库,则图像不会保存到文件夹,反之亦然。我曾多次尝试过一些变化,但无法做到。 任何帮助将不胜感激。提前谢谢......

    String saveFile="";
        String contentType = request.getContentType();
        if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        while(totalBytesRead < formDataLength){
        byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
        totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);
        saveFile = file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1,contentType.length());
        int pos;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
        File ff = new File("C:/Users/Snigdha/Documents/NetBeansProjects/new_signups_recent/new_signups_1/web/pic/"+saveFile);
        FileOutputStream fileOut = new FileOutputStream(ff);
        fileOut.write(dataBytes, startPos, (endPos - startPos));
        fileOut.flush();
        fileOut.close();
 response.getWriter().write("Uploading done");
        sess.setAttribute("image_name", ff.getName());
        response.sendRedirect("signup_forms.jsp");
        }

0 个答案:

没有答案