This article mainly introduces the implementation of ajax image upload based on firefox. Everyone knows that using ajax under FF is very difficult. Today I will introduce to you a perfect solution to this problem. Friends can refer to it.
There are many protocols for uploading image files. This time we mainly talk about the form of "Content-Type: multipart/form-data;".
At work, some static files in the frontend are referenced through the FTL template system, and the frontend page is referenced through SSI. During project development, a large number of static SHTML files need to be generated. I originally thought it should be a backend thing, but Ever since I joined this company, this fast thing has been handed over to the front end~~ I was speechless, I had no choice but to follow my habits, but I am a lazy person, so I just thought about whether I could submit it through ajax
Based on my usual reading of MDN, today it is mainly the FormData object that solves the protocol for uploading multiple files.
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/FormData
Everything seems to be perfect, but in the File method, only on the chrome level to run. There is no way to upload pictures. Generally, ordinary files can be implemented through Blob objects.
The following are some methods to achieve it, an exploration process
1. After looking for a long time, I found a method to convert URL to File object. , all failed. . .
2.HTML5 File direction
a. Let’s start with the example of HTML5 File
https://developer.mozilla.org/en-US/docs/DOM/ FileReader
In the example, when printing oFREvent.target.result, only "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQ..."
is found. It turns out to be base64 encoded. Data
When I saw the encoded data above, I wondered if the Blob object was supported. The code is as follows
var fd = new FormData(); fd.append('my-file', new Blob(["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQ..."],{type:"image/jpeg"})); fd.append('my-file', new Blob(["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQ..."],{type:"image/jpeg;base64"}));
b. I thought that base64 had been encoded. Is it possible to decode it through the method?
I found an example of base64 encoding and decoding on the Internet, and used it
fd.append('my-file', new Blob([new Base64().decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQ...')],{type:"image/jpeg"}));
After capturing the packets, I found that the normal file transfer is different from the ajax data, so it must have failed
3. For the sake of security at the browser page level, it seems that the operation of selecting files must be done manually
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/FormData/Using_FormData_Objects
fd.append('my-file',document.querySelector(Selector).files[0])
In fact, I was misled by the File api at the beginning. I always thought it was at the page level.
Suddenly I discovered that this method was used at the top of the last address. . It seems that I still didn’t look at things seriously and took some detours.
Postscript, by adding functions like the Oil Monkey plug-in and supporting the chrome-level File method, we can easily upload photos.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax loading external page pop-up layer effect implementation method
AJAX submission form data example analysis
Ajax cross-domain (same basic domain name) form submission method
The above is the detailed content of Ajax image upload based on firefox. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!