javascript - Ajax文件上传表单在firefox中有效,但在safari中无效

标签 javascript jquery ajax

我编写了以下代码来从网络浏览器上传文件。它在 Firefox 中有效,但在 Safari 中无效,是否有任何明显的原因导致这种情况。

// when the file field is changed I get its data and the "salonId" variable
    $("#btnFrontUpload").change(function (e){
        frontPic = e.target.files[0]
        displayPicAsBackground(frontPic, "btnFrontUploadShow")
        frontPicName = frontPic.name
        salonId=$("#salonId").val();
    });

fd = new FormData()

$("#btnNextPhotos").click(function(){
    $('#mdlPhotos').modal('hide')
        resizeAndPopulateVariables()
        doAjaxRequest()
    });

});

function updateMilliTime(){
    milliTime = (new Date).getTime()
}

function displayPicAsBackground(file, btn){
// here I display the uploaded file (which is a picture) on the screen
//  it works on most browsers including mobile safari, but not the "desktop" safari
    $.canvasResize(file,
        {
            width: 160,
            height: 0,
            crop: false,
            quality: 100,
            callback: function (data)
            {
                $('#'+btn).css("background", "url("+data+")")
            }
        });
}

function resizeAndPopulateVariables(){
// I resize the image and create a file variable for upload
    $.canvasResize(frontPic,
           {
                width: 400,
                height: 0,
                crop: false,
                quality: 100,
                callback: function (data)
           {    // Add file data
                var frontPicForUpload = $.canvasResize('dataURLtoBlob', data)
                fd.append("frontPic", frontPicForUpload)
                fd.append("frontPicName", frontPicName)
                fd.append("salonId", salonId)
           }
           });
}

function doAjaxRequest(){
// send the ajax request
    $.ajax(
          {
                url: '/create/picture',
                type: 'POST',
                data: fd,  //fd is a global variable
                dataType: 'json',
                contentType: false,
                processData: false,
                beforeSend: function (xhr)
                {
                    xhr.setRequestHeader("pragma", "no-cache");
                }
          }
          ).done(function (response){
            window.location.reload()
    });

最佳答案

我真的不知道你的问题可能是你的 Ajax cal,所以为什么不使用 plugin Ajax 。我知道这听起来很压抑。

这是一个非常简单的示例:

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); 
    </script> 
</head> 

关于javascript - Ajax文件上传表单在firefox中有效,但在safari中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15183457/

相关文章:

jquery - 从上到下过渡不透明文本

ajax - jsf commandLink 和 f :ajax partial rendering 的奇怪行为

javascript - 如何使所有链接动态加载页面? (甚至动态加载页面中的链接)

php - 如何将ajax排序结果放入html div中

javascript - 在 React JSX 中使用 if 语句

javascript - Meteor HTTP 从响应中获取 cookie

php - jQuery 拖放 - 将位置保存到 MySQL 数据库中

javascript - 如何使用 jquery/javascript 在 asp.net c# 中禁用单选按钮单击上的链接按钮

javascript - 全日历事件默认显示显示开始时间

javascript - 无法使用 jQuery 委托(delegate)滚动事件