CopyPastor

Detecting plagiarism made easy.

Score: 0.8026331106151104; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2023-01-27
by Alex

Original Post

Original - Posted on 2013-01-25
by Techie



            
Present in both answers; Present only in the new answer; Present only in the old answer;

If relying on the filenames is enough, you can try to store them to check if it has been uploaded already : ```js const [files, setFiles] = useState([]);
const [fileNames, setFileNames] = useState([]);
const onSelectFile = (e) => { try { let fileArr = cloneDeep(files); let promises = []; for (let file of e.target.files) { promises.push( new Promise((resolve, reject) => { const fileName = file.name //if the file has not been already uploaded if (!fileNames.includes(fileName)) { //add the current fileName in state setFileNames([fileName, ...fileNames]); const type = file.type; let reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function (evt) { const fileData = evt.target.result; fileArr.push({ name: fileName, type: type, data: fileData, comment: "", id: `${new Date().getTime()}_${fileName}`, canDelete: true }); if (typeof props.onFileSelected == "function") props.onFileSelected(fileArr); resolve(true); } reader.onerror = function (evt) { console.log("error reading file"); reject(false); } } else { alert("File has already been uploaded"); reject(false); } }) ); }
Promise.all(promises).then(r => { setFiles(fileArr); }) } catch(e) { console.log(e); } }
```
Note: this will not prevent the case when the user upload a file, then refresh the website and upload the same file again If you want to prevent that you have to ask your backend if the file has already been upload or not.
I have been using the below script to upload images which happens to work fine.
#HTML <input id="file" type="file" name="file"/> <div id="response"></div>
#JavaScript
jQuery('document').ready(function(){ var input = document.getElementById("file"); var formdata = false; if (window.FormData) { formdata = new FormData(); } input.addEventListener("change", function (evt) { var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) { file = this.files[i];
if (!!file.type.match(/image.*/)) { if ( window.FileReader ) { reader = new FileReader(); reader.onloadend = function (e) { //showUploadedItem(e.target.result, file.fileName); }; reader.readAsDataURL(file); }
if (formdata) { formdata.append("image", file); formdata.append("extra",'extra-data'); }
if (formdata) { jQuery('div#response').html('<br /><img src="ajax-loader.gif"/>');
jQuery.ajax({ url: "upload.php", type: "POST", data: formdata, processData: false, contentType: false, success: function (res) { jQuery('div#response').html("Successfully uploaded"); } }); } } else { alert('Not a vaild image!'); } }
}, false); });
#Explanation
I use response `div` to show the uploading animation and response after upload is done.
Best part is you can send extra data such as ids & etc with the file when you use this script. I have mention it `extra-data` as in the script.
At the PHP level this will work as normal file upload. extra-data can be retrieved as `$_POST` data.
Here you are not using a plugin and stuff. You can change the code as you want. You are not blindly coding here. This is the core functionality of any jQuery file upload. Actually Javascript.

        
Present in both answers; Present only in the new answer; Present only in the old answer;