duanjihe5180 2019-01-06 16:44 采纳率: 100%
浏览 83
已采纳

热到图像预览到服务器后自动上传图像

i got problem in order to upload my image, after sucessfull image preview in jquery to my server. It shows file is not defined in the form.append statement or action.

Tried to fix it on my own but couldn`t find a solution that works.Could you advice me or help me a little bit code is following.

function readURL(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
        $('#imagePreview').css('background-image', 'url('+e.target.result +')');
        $('#imagePreview').hide();
        $('#imagePreview').fadeIn(650);
    }
    reader.readAsDataURL(input.files[0]);
}
}
$("#imageUpload").change(function() {
readURL(this);

var formData = new FormData();
formData.append('formData', file);
$.ajax({
url: 'assets/uploadProfilepic.php',  //Server script to process data
type: 'POST',
data: formData,
contentType: false,
processData: false,
//Ajax events
success: function(html){
    alert(html);
}
});
});

The HTML Part

<div class="avatar-upload">
        <div class="avatar-edit">
            <input type='file' id="imageUpload" accept=".png, .jpg, .jpeg" name="file" />
            <label for="imageUpload"></label>
        </div>
        <div class="avatar-preview">
            <div id="imagePreview" style="background-image: url(http://i.pravatar.cc/500?img=7);">
            </div>
        </div>
    </div>

Uncaught ReferenceError: file is not defined but it should be or i dont really understand

Please someone could help me a little bit or give a solution. Thanks and Greets

  • 写回答

1条回答 默认 最新

  • dor65412 2019-01-06 18:25
    关注

    The file variable has no defined value in your callback, so it will use file from global scope, which has value undefined. In strict mode, accessing a variable which has not been declared in local scope raises an error.

    Be sure that just below your call to readURL(this) that file is assigned to the first file sent by the client (i.e. this.files[0]). And to append a file using the FormDatas:

    // The first argument ("file") is the POST key for the file
    // The file metadata can be retrieved using $_FILES['file']
    
    // The second argument is the file uploaded by the user
    // The third argument is the name of the file as perceived by the server
    var formData = new FormData();
    formData.append("file", file, "avatar.png");
    

    Keep it mind that to upload files (including avatars), the form should be using the attribute enctype="multipart/form-data", so that the browser can send properly the image file.

    And implementing a file upload is not all; it is recommended that you validate the file server-side (with PHP, you can use the mime_content_type function, which requires the fileinfo extension). Before doing anything, please read this page on POST file uploads.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料