How to upload multiple media files "image or video" for RESTFull API in Laravel 5.7? - laravel-5.7

I'm working on RESTFul API Project in Larave 5.7, and I'm using postman to test my APIs, I'm trying to upload multiple images or videos but nothing work with me!
I tried this way, however, it never gets inside the foreach loop!
And I also tried to use allFiles instead of file but it gives me only one file (maybe the last selected one)!!
foreach ($files as $file) {
$extension = $file->getClientOriginalExtension();
$check = in_array($extension,$allowedfileExtension);
if($check) {
foreach($request->fileName as $mediaFiles) {
// save here
}
} else {
return response()->json(['invalid_file_format'], 422);
}
return response()->json(['file_uploaded'], 200);
}

I was uploading the files in a wrong way with Postman!
This is the correct way :D
It's worked :D

Related

File field is showing that my previously uploaded files are 0 bytes

Got a bit of a weird issue here. I recently started doing maintenance on a website that I did not originally build. The Drupal site has a bunch of nodes with an audio file field, and a jQuery player that plays them. On a lot of the nodes the player does not load, and I've realized this is because the file is reported as being 0 bytes when I edit the node. I'm not sure why this is. At first I was thinking it might be a file permissions thing, but I don't think thats the case as the permissions look fine to me. To fix it all I had to do was re-upload the file. Problem is that there are hundreds of these files and I'd like to fix it just by making one update if that is possible.
Here is a working version of Terry's pseudocode for Drupal 7.
$fids = db_query('SELECT fid FROM {file_managed} WHERE filesize = 0')->fetchCol();
foreach(file_load_multiple($fids) as $file) {
// Get full path to file.
$target = drupal_realpath($file->uri);
// If the file does not exist try the next file.
if (!file_exists($target)) {
echo "File $file->uri does not exist." .PHP_EOL;
continue;
}
// Find and store size of file
$file->filesize = filesize($target);
// Size of file is
if ($file->filesize > 0) {
file_save($file);
}
else {
echo "Size of $file->uri is still zero." . PHP_EOL;
}
}
Run it with drush:
drush php-script fix-file-sizes.php
We had the same problem: a lot of 0 byte files in the database.
What we did looked something like this:
function update_my_filesizes() {
$fileIDs = db_query('SELECT fid FROM {file_managed) WHERE filesize = 0')->fetchCol();
foreach(file_load_multiple($fids) as $file) {
// determine size of file
$filesize = SIZE_OF_THE_FILE; (pseudocode, we had to use filesize())
$file->filesize = $filesize;
if($file->filesize > 0) {
file_save($file);
} else {
echo "Filesize for file <filename here> is still 0 :(";
}
}
}

multiple file upload symfony 2

How to use multiple files upload in symfony? I'm not really
sure how to do it 'the symfony way'. I read How to handle File Uploads with Doctrine, but how upload many files?
I try use collection field type and add setFiles method to object model
public function setFiles($files)
{
$this->files = $files;
foreach ($files as $file) {
$file->setObject($this);
}
}
but have exception (this always worked for normal model without any files)
Fatal error: Call to undefined method
Symfony\Component\HttpFoundation\File\UploadedFile::setObject()
please help me.
UPDATED:
I use for main object second form with file, and...
$files = $this->getRequest()->files->get('val_image');
foreach($files as $file){
$foto = new Foto;
$foto->setFile($file);
$foto->setPremises($premises->getPremises());
$this->getEm()->persist($foto);
}
it's works :)
Maybe this plugin can help you:
https://github.com/caoimhin/Symfony-jQuery-File-Upload/blob/master/Resources/public/README.md

Flex : How to start a flex/air app by dragging a file to the application Icon and got the file

I'm building a flex application
It has a functionality that can upload files to server
The customer will want to open app by dragging file to the icon and the app will start and begin uploading. But I cannot find how to implement that
Can anyone help me? Any help or link will be great appreciated.
Edit: thanks to alxx
Here is my code
private function initComponents():void {
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvokeEvent);
}
public function onInvokeEvent(invocation:InvokeEvent):void {
arguments = invocation.arguments;
currentDir = invocation.currentDirectory;
if(arguments.length > 0)
{
var dfile:File = new File(arguments[0]);
if(dfile.exists) {
if(ArrayUtil.getItemIndex(dfile.extension,ConfigManager.AllSupportedFileExtensions) > -1)
uploadQueue.push(dfile);
if(!logged)
Alert.show("Please login before upload");
}
}
}
Try listen to InvokeEvent.INVOKE of Application, as described here. You'll get full native path to dropped file in arguments array (just tested it myself.)

WMF image data validation?

There is an image capturing device which gives its output in wmf. This output is stored in the database directly. We have cases where at times some of these images do not appear on a web page in IE. But if we right click on the page we are able to save the image on to the hard disk; meaning the image does exist on the page, but does not appear visible. I think this is because of some file corruption issue, but I don't know how to resolve it. We are however able to view such files using MS Picture Viewer (desktop app). Is there anyway we can detect such problematic files?
I hope I am not being over simplistic over this but the following function works for me:
public bool IsValidMetaFile(string filePath)
{
try
{
var metaFile = new Metafile(filePath);
var metaFileHeader = metaFile.GetMetafileHeader();
return metaFileHeader.IsWmf() ||
metaFileHeader.IsWmfPlaceable() ||
metaFileHeader.IsEmf() ||
metaFileHeader.IsEmfPlusDual() ||
metaFileHeader.IsEmfPlusOnly() ||
metaFileHeader.IsEmfOrEmfPlus();
}
catch (Exception mesg)
{
return false;
}
}

FileReference.load() does not as excepted

I used Flash player 10, and Flex SDK 3.4. The code as followings:
// Following comes callbacks
function imageLoadOpenCallback(evt:Event):void
{
trace("in--open");
}
function imageLoadCompleteCallback(evt:Event):void
{
trace("in--load");
var fr:FileReference = evt.target as FileReference;
trace(fr.data);
}
function imageLoadErrorCallback(evt:IOErrorEvent):void
{
trace("in--ioerror");
}
function imageSelectCancelCallback(evt:Event):void
{
trace("in cancel");
}
function imageSelectCallback(evt:Event):void
{
trace("in -- select");
for (var i:int=0; i<frl.fileList.length; i++)
{
frl.fileList[i].addEventListener(Event.OPEN, imageLoadOpenCallback);
frl.fileList[i].addEventListener(Event.COMPLETE, imageLoadCompleteCallback);
frl.fileList[i].addEventListener(IOErrorEvent.IO_ERROR, imageLoadErrorCallback);
frl.fileList[i].load();
trace(frl.fileList[i]);
trace(frl.fileList[i].creationDate);
trace(frl.fileList[i].creator);
trace(frl.fileList[i].data);
trace(frl.fileList[i].name);
}
}
// Following comes UI handlers
function onAddPictures():void
{
var imageFilter:FileFilter = new FileFilter("Images", "*.jpg;*.png");
frl.addEventListener(Event.SELECT, imageSelectCallback);
frl.addEventListener(Event.CANCEL, imageSelectCancelCallback);
frl.browse([imageFilter]);
}
Only the imageSelectCancelCallback handler get called when I select some files in the dialog. But no load/open/io_error handler get called at all. I have Google some code example, in which it used FileReference instead of FileReferenceList. I don't know the reason, could you please help me?
In Air the fileReference objects in fileReferenceList do not fire the complete event when doing fileList[i].load(). In a Flex project it works fine. Adobe has not responded to bug reports on this appropriately.
Make sure in your compiler settings for flex, that you have at least 10.0.0 for "Use a specific version".
The main reason to use FileReferenceList instead of FileReference would be if you need to upload multiple files at once. If you only want to allow uploading one file at once, simply use FileReference.
Some clarification: imageSelectCallback(), and NOT imageSelectCancelCallback(), should get called when you select some files in the file browser AND click OK. imageSelectCancelCallback() is only called when you click Cancel.
Other than that, I never used the load() API, but I did use the upload(URLRequest) API. I am not sure what's your use case, but if you need to upload an image to a server, you should use the upload() method.
Speaking of upload events, I experienced some reliability issues when listening to Event.COMPLETE events, so I actually got better results listening to DataEvent.UPLOAD_COMPLETE_DATA.

Resources