簡體   English   中英

如何使用帶有 json 和 php 的 jquery 的 $.ajax 函數上傳文件

[英]How can I upload a file using jquery's $.ajax function with json and php

我正在嘗試使用 jQuery 的$.ajax函數上傳文件,但沒有得到任何輸出。 有人請幫我解決這個問題。 我不知道這個腳本是否正確。 我的腳本是:

$.ajax({
  url:'newsup.php',
  data: "",
  type: 'POST',
  contentType:'multipart/form-data',
  dataType: 'json',
  catche: 'false',

  success:function(data6)
  {
    $("#disp").removeClass().addClass((data6.error=== false)? 'success':'error').html(data6.msg).fadeIn('fast');
    //dele();
    if($("#disp").hasClass('success'))
    {
      alert("success");
      setTimeout("$('#disp').fadeOut('slow')",3000);            
    }
  },

  error:function(XMLHttpRequest,textStatus,errorThrown)
  {
    $("#disp").removeClass().addClass('error').html("There was an <strong>"+errorThrown+"</strong> error due to  <strong>"+textStatus+" condition").fadeIn('fast');
  }              

});

此外,我需要幫助使用 jQuery 從文件上傳字段獲取數據。

請為此使用插件。
在我看來,這個插件是更好的解決方案。您不需要記住所有選項等。只需將您的“ajax”替換為“ajaxForm”。

請閱讀下面的例子
http://jquery.malsup.com/form/#ajaxForm

我就是這樣做的。 使用 FormData 對象。

注意:for 語句的奇怪語法只是將 "f" 設置為 array[i] 實例。

        $("#submit").click(function () {
            var formData = new FormData();
            for (var i = 0, f; f = fileArray[i]; i++) {
                formData.append("opmlFile", f);
            }
            $.ajax({
                url: "/Documents/SaveFiles/" + @Model,
                type: "POST",
                data: formData,
                cache: false,
                contentType: false,
                processData: false
            })
            .error(function (xhr, status, error) {
                $.notify(error, true);
            })
            .success(function (data, status, xhr) {
                $.notify("Success");
            });
        });

不幸的是,我不記得這是從哪篇文章中得到的,但它是 Stack Overflow 上的其他人。

AJAX 不支持文件上傳。 有像ajaxfileupload這樣的插件,它基本上創建一個隱藏的表單並動態上傳你的文件。

看看這里並閱讀 Oli 的回答

我正在使用它並且它工作正常:

  $('#btnUploadFile').on('click', function () {
                var data = new FormData();
                var files = $("#fileUpload").get(0).files;

                // Add the uploaded file content to the form data collection
                if (files.length > 0) {
                    data.append("upload", files[0]);
                }

                // Make Ajax request with the contentType = false, and procesDate = false
                var ajaxRequest = $.ajax({
                    type: "POST",
                    url: "/api/documents",
                    contentType: false,
                    processData: false,
                    data: data,

                    error: function (xhr, status, error) {
                        console.log(xhr);
                        console.log(status);
                        console.log(error);
                        console.log(data);
                    }
                });

                ajaxRequest.done(function (xhr, textStatus) {
                    $("#response").attr('class', "alert alert-success");
                    $("#response").html("File uploaded successfully");
                });


            });

您可以使用Jquery File Upload Plugins 1Jquery File Upload Plugins 2這兩個插件中的任何一個,並且此腳本沒有錯誤。

希望能幫助到你

謝謝,拉希德

Ajax 支持使用 FormData 對象上傳文件,也支持除 IE8/9 之外的所有主流瀏覽器 見下文

https://developer.mozilla.org/en-US/docs/Web/API/FormData

另一種選擇是對文件內容進行 base64 編碼並將其作為字符串發送,在后端對其進行解碼。

只需在表單上使用提交事件即可發送文件並防止默認表單操作

$('#form').submit(function(e) { return false; });

並通過服務器端獲取文件

$_FILES['inputName'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM