Answer the question
In order to leave comments, you need to log in
Uploading an image before uploading it. How do they do it?
For example, a post is being made. And an image is added to it. But it is loaded before the publication of the post itself (while the text is being typed, the picture is already visible). That is, by the time the post itself is saved, the picture is already loaded, as it were. But if the post is not saved, then probably the picture will not be saved.
How it's done? How does is called? Where to read?
Example: discourse - post creation
Answer the question
In order to leave comments, you need to log in
To get started, decide what you really need - a preview of the image (to make it look like the image is loaded, the user sees it in the content that he writes) or the actual upload of the image to the server. There will be different techniques depending on the answer.
Consciously consider and weigh the main disadvantages of uploading to the server. The most important thing is that pictures on the server will accumulate and you will have to develop a system for determining "necessary" and "unnecessary" as well as a mechanism for automatically removing unnecessary ones. Because there will be a lot of garbage. Someone wrote a draft and did not publish / closed the window and scored, and the picture (or even several) was already loaded. Someone uploaded one picture, looked, did not like it, chose and uploaded another one. And so 10 times in a row until I picked up a picture that I like. And you have 9 unnecessary pictures loaded on the server. After a while your downloads folder grows to gigabytes/thousands of files, you look at it and don't really understand what is being used and what is not.
Based on this, in most cases, all you need is a client-side preview with javascript. You can do this with vanilla js or use an adequate library like https://github.com/blueimp/JavaScript-Load-Image which already solves a bunch of small tasks that you don’t even know about yet (for example, automatic rotation by metadata, resize preview for purposes performance optimization, crossorigin, etc.).
If, nevertheless, you decide to load from directly to the server, then start with the minimum technical specification. Are you using any CMS or framework. Where and how will you upload files. How will you store the history of downloaded and clear unnecessary ones. Etc. The upload process itself is trivial - you must have a URL that accepts data (image + any useful information, for example ID / UUID of the draft entry for which the picture is uploaded) using the PUT or POST method, processes them and returns the image URL. Send a picture there with Ajax, get a response with the address of the picture on the server, insert it into the DOM.
I did.
A hidden frame is drawn:
<iframe width="0" height="0" name="iframe"></iframe>
Форма для загрузки изображений с кнопкой выбора файла:
<form action="/thumbnail.php" method="post" enctype="multipart/form-data" target="iframe">
<input type="file" name="file" id="file" />
</form>
<input type="hidden" name="thumbnail[]" value="123">
id | id_post | file_name | file_date
123 | null | file.jpeg | 20-02-2020
update images set id_post = 1 where id = 123
The Aleksey option is more suitable for image previews. I would recommend doing this:
1. The user selects an image, you preview it and send it to the server via ajax
2. The server gives you the id or file name for the uploaded image
3. After the user creates a post (or whatever you need) you send along with other data id pictures
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question