Symfony2 cannot get mp3 or mp4 file from request - symfony

I use symfony2 to build a website that is similar to Youtube, but I got failed when I upload .mp3 and .mp4 files into my sever.
In my twig file, it looks like,
<form class="uploadForm" method ="POST" enctype="multipart/form-data" action="{{path('mss_core_media_uploadAudio')}}">
<div class="col-md-6">
<h2>Upload center</h2>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="input-group" style="width: 420px;">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="audioFile"></span>
Remove
</div>
</div><button class="btn btn-primary fileupload-exists" type="submit">Upload</button>
</div>
</form>
my controller code looks like,
$audio = $request->files->getData()->get('audioFile');
$status = 'success';
$uploadedURL = '';
$message = '';
$logger = $this->get('logger');
$logger->debug($audio);
if (($audio instanceof UploadedFile) && ($audio->getError() == '0')) {
But I got $audio is null in my action method of controller, so the rest part of code cannot be performed. I have no idea about this, I was confused that I can successfully upload an image and a pdf file by using the same method, but both audio and video files got failed.

Related

How can I send list from view to controller (ASP.NET MVC)?

I want to make export file using
ActionResult Download(List<UserProjectPercentReportViewModel> percentsByProjectsAndUsers)
method. But I can not pass a parameter. I try to send it like this:
<div class="col-md-2">
<p> </p>
<p><input type="button" onclick="location.href='#Url.Action("Download",new { percentsByProjectsAndUsers=Model})'"
class="btn btn-info" value="Export" /></p>
</div>
But I get percentsByProjectsAndUsers.Count() == 0 in my method.
When I tried to send just string, it works, but I need to pass a List<>.

Why is my ptzloc1 button below not working?

Sorry if I'm posting this code fence wrong. I am a senior citizen (noob) writing my first html.
I am writing a short script to control a ptz ip cam (192.168.1.247) for my raspberry pi.
The ptzLeftSubmit() and ptzRightSubmit() both work when I hit the buttons that call them.
The ptzloc1 and ptzloc2 do not work when I hit them.
The ptzloc1 and ptzloc2 html param.cgi call works in my browser window but not when I push the button.
Am I not allowed to pass the cmd parameter in a script? What am I doing wrong with ptzloc1 ptzloc2 etc?
function ptzLeftSubmit()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/ytleft.cgi";
form1.submit();
}
function ptzRightSubmit()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/ytright.cgi";
form1.submit();
}
function ptzloc1()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-status=1&-number=0";
form1.submit();
}
function ptzloc2()
{
form1.action="http://192.168.1.247/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-status=1&-number=1";
form1.submit();
#then later I do this
<body onLoad="load()">
<form name="form1" method="get" target="test">
</form>
<div style = "position:absolute; left:80px; top:20px;">
<p>
<input type="button"; value="∧" name="B3" onClick="ptzUpSubmit()">
<input type="button" ; value="∨" name="B2" onClick="ptzDownSubmit()">
<input type="button" ; value="<" name="B1" onClick="ptzLeftSubmit()">
<input type="button" ; value=">" name="B0" onClick="ptzRightSubmit()">
<button type="button" ; name="B4" onClick="location.reload()"><script type="text/javascript">document.write("REFRESH");</script></button>
</p>
</div>
<div style = "position:absolute; left:400px; top:20px;">
<p>
<input type="button"; value="1" name="B4" onClick="ptzloc1()">
<input type="button"; value="2" name="B5" onClick="ptzloc2()">
#etc....

ASP.NET how to pass value from form to another page

i have page with the url that show my list of items:
https://localhost:123/AllSystems/List?systemTypeID=1
and inside this page i have search (i pass SearchTerm to list.cshtml.cs file ):
<form method="get">
<div class="form-group">
<div class="input-group">
<input type="search" class="form-control" asp-for="SearchTerm" />
<button type="button" class="btn btn-danger">
<span class="glyphicon glyphicon-search"></span> **Search**
</button>
</div>
</div>
</form>
when i click on Search button (inside the search form) i go to url:
https://localhost:123/AllSystems/List?SearchTerm=nba
that fine but i want to pass the systemTypeID=1 to from last url
(https://localhost:123/AllSystems/List?systemTypeID=1).
how i do it?
i want the result be something like that:
https://localhost:123/AllSystems/List?systemTypeID=1&SearchTerm=nba
thank's
you can pass multiple dynamic values to QueryString as below
you can set Value1 and Value2 and pass that values to your query string as below
string value1="1";
string value2="nba";
Response.Redirect("https://localhost:123/AllSystems/List?systemTypeID"+value1+"&SearchTerm="+value2);
in second page you can access the value like
string typeid= Request.QueryString["systemTypeID"];
string searchvalue = Request.QueryString["SearchTerm"];

How to ignore specific fields in form in thymeleaf?

I'm creating a form in thymeleaf that contains a file upload field that's not part of my model.
When I load the page, thymeleaf complains and throws NotReadablePropertyException for that field.
How can I get thymeleaf to ignore the fact that the field does not exist on the model?
Code:
<div class="form-group"
th:classappend="${#fields.hasErrors('uploadFile')}? 'has-error'">
<label class="col-sm-12 control-label" style="text-align: left; margin-bottom: 7px;">Upload Photo</label>
<div class="col-sm-12" style="margin-bottom:5px;">
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select file</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="uploadFile" accept="image/*" required>
</span>
Remove
</div>
<p id="error" style="position:absolute;color:#FF0000;margin-top:-7px"></p>
</div>
</div>
Error:
org.springframework.beans.NotReadablePropertyException: Invalid property 'uploadFile' of bean class [bean.Library]: Bean property 'uploadFile' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
the error that you get is because of the line
th:classappend="${#fields.hasErrors('uploadFile')}
that expects a field expression as a parameter.
You can replace
th:classappend="${#fields.hasErrors('uploadFile')}
with
th:classappend="${#fields.hasErrors('*')}
where the has-error class will appear if there is an error with any of the fields.
Or you can even replace it with
th:classappend="${#fields.hasErrors('global')}
that is not associated with any specific field in the form.
Or alternatively, you can add the field (uploadFile) in the model as a transient attribute.

How to upload an image using Sonata Media Bundle

I am working on a project using symfony.I want to use Sonata Media bundle,in order to upload image.
Unfortunately I don't knwo how to use it or how to start.
I havethis form :
<form action="" method="POST" class="filePhotoForm form-inline" role="form">
<div class="form-group">
<input type="button" class="btn btn-start-order browse" value="Browse">
<input type="text" class="form-control file-name" readonly="readonly" placeholder="No file selected">
<input type="file" name="filePhoto" id="filePhoto" class="hidden file-upload">
</div><br/><br/>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And the code in controller:
public function changePictureAction(Request $request)
{
return $this->render('MedAppBundle:Profile:change_picture.html.twig');
}
Can you help with the basics of uploading?
Thank you!
i have made a custom function for that
protected function saveImageMediaBundle($file,$context = 'default')
{
$mediaManager = $this->get('sonata.media.manager.media');
$media = new Media();
$media->setContext($context);
$media->setProviderName('sonata.media.provider.image');
$media->setBinaryContent($file);
$mediaManager->save($media);
return $media;
}
and get file by this
$file = $request->files->get('newsImage',null);
$media = $this->saveImageMediaBundle($file,'news');

Resources