본 사이트는 여러분의 사용자 경험을 개선하기 위해 유지 보수를 진행하는 동안 기능이 제한됩니다. 도움말로 문제가 해결되지 않고 질문을 하고 싶다면 Twitter의 @FirefoxSupport 및 Reddit의 /r/firefox 채널을 활용하세요.

Mozilla 도움말 검색

고객 지원 사기를 피하세요. 저희는 여러분께 절대로 전화를 걸거나 문자를 보내거나 개인 정보를 공유하도록 요청하지 않습니다. "악용 사례 신고"옵션을 사용하여 의심스러운 활동을 신고해 주세요.

자세히 살펴보기

appending file data to formData in javascript not working

  • 5 답장
  • 4 이 문제를 만남
  • 1 보기
  • 최종 답변자: JeffAgee

more options

The following code is not working in Firefox 21 even though it corresponds to Mozilla docs on how to implement FormData in ajax. (The code DOES work in both Chrome and Opera).

var data = new FormData(document.getElementById("uploadform")); $.each($('#file')[0].files, function(i, file) { data.append('file-'+i, file, this.name); });

The $.each loop is where it's bogging down because I put an alert in there which popped up in Chrome and Opera, but not in firefox.

Here's the apache php error that occurs:

[Wed Jun 05 01:48:44 2013] [error] [client ::1] PHP Notice: Undefined index: file-0 in /home/Jeff/public/main/public/upload.php on line 7, referer: http://localhost/

So, basically, in Firefox, the file array is not being appended to the FormData object within the javascript code, then when the form data is submitted to the server it's throwing the apache php error.

Anyone with any inklings?

The following code is not working in Firefox 21 even though it corresponds to Mozilla docs on how to implement FormData in ajax. (The code DOES work in both Chrome and Opera). var data = new FormData(document.getElementById("uploadform")); $.each($('#file')[0].files, function(i, file) { data.append('file-'+i, file, this.name); }); The $.each loop is where it's bogging down because I put an alert in there which popped up in Chrome and Opera, but not in firefox. Here's the apache php error that occurs: [Wed Jun 05 01:48:44 2013] [error] [client ::1] PHP Notice: Undefined index: file-0 in /home/Jeff/public/main/public/upload.php on line 7, referer: http://localhost/ So, basically, in Firefox, the file array is not being appended to the FormData object within the javascript code, then when the form data is submitted to the server it's throwing the apache php error. Anyone with any inklings?

글쓴이 JeffAgee 수정일시

모든 댓글 (5)

more options

Did you check Firefox's error console (Ctrl+Shift+j) or Web Console (Ctrl+Shift+k) for errors in the script itself?

In this function, are you getting the expected data types for i and file, or are you saying this function never runs?

function(i, file) {data.append('file-'+i, file, this.name);}

(For reference: Using FormData Objects - MDN)

more options

Hi jscher. Thanks for the reply.

I simplified the code (removed the loop because I only need to upload one file at a time (but still have the problem):

var data = new FormData(document.getElementById("uploadform"));

var selected_file = document.getElementById('file').files[0];

alert(selected_file);

data.append('file-0', selected_file);

The alert is just a tester--In Firefox, the alert does not go off, but it does in Chrome and Opera.

Here's the data from the Firebug console on the http post:


327999323633775102168260978 Content-Disposition: form-data; name="MAX_FILE_SIZE" 1000000 -----------------------------327999323633775102168260978 Content-Disposition: form-data; name="file"; filename="" Content-Type: application/octet-stream -----------------------------327999323633775102168260978 Content-Disposition: form-data; name="file-0" undefined -----------------------------327999323633775102168260978--


here'sthe http response data:

Invalid file

I could post the header info too if that would help. For the life of me I don't understand what is going wrong. I've even tested this out on both linux machine and windows machine--same result. And same result on local LAMP and remote LAMP at linode.com, so it's not some kind of virus or something weird I don't think.

more options

Oh, I almost forgot to say that no I'm not getting any errors in the console.

more options

I don't know why the alert() wouldn't fire and yet have no error in the console. Can you use the Firefox Debugger tool or Firebug Scripts panel to set one or more breaks and see whether that part of the code actually is running?

(Cross-reference to thread on StackOverflow: jquery - Javascript FormData function works with Chrome and Opera but not Firefox - Stack Overflow)

글쓴이 jscher2000 - Support Volunteer 수정일시

more options

Ok. I my testing must be getting screwy, sorry.

I just ran it with the above code exactly like I wrote last, using "alert(selected_file)" and I DO get an alert box in Firefox that says "undefined", but I get no console error.

I then ran it again and changed the alert slightly to: alert(selected_file.name) and I do NOT get an alert in Firefox, but DO get a console error that says:

TypeError: selected_file is undefined [Break On This Error]

alert(selected_file.name);

In Chrome I get an alert both times with no console errors. First alert says "[object File]", second one gives the file name like "plant.jpg".