所以我有一些功能代码,但我需要向其中添加多个图像上传。我用了this plugin .现在我的 HTML 看起来像这样:
<form class="form-horizontal full-width pull-left m-t-20" name="myForm" ng-if="showForm">
<div class="form-group">
<label for="Naam" class="col-sm-3 control-label">Naam</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="Naam" ng-model="addForm.name" placeholder="Naam">
</div>
</div>
<div class="form-group">
<div class="imageselect col-sm-3">
Afbeeldingen
<input type="file" ngf-select ng-model="files" ngf-multiple="true" accept="image/*" />
Drop afbeeldingen: <div ngf-drop ng-model="files" class="drop-box">Drop</div>
</div>
<div class="col-sm-9">
<div ng-repeat="file in files" class="imagepreview">
<img ng-show="myForm.file.$valid" ngf-thumbnail="file" class="thumb"><br /> <button ng-click="file = null" class="thumbremove" ng-show="file">Verwijder</button>
</div>
</div>
</div>
<div class="form-group">
<span class="progress" ng-show="files.progress >= 0">
<div style="width:<< files.progress >>%" ng-bind="files.progress + '%'"></div>
</span>
<div class="col-sm-10">
<button type="submit" ng-click="addStore(files)" class="btn btn-default">Opslaan</button>
</div>
</div>
</form>
这是我的javascript代码:
.controller('StoresController', ['$scope', '$http', 'handlerService', 'Upload', '$timeout', function($scope, $http, handlerService, Upload, $timeout) {
$scope.model = {};
$scope.model.stores = stores;
$scope.showForm = true;
$scope.addForm = {};
$scope.addForm.name = '';
$scope.addStore = function(files) {
$scope.showForm = true;
files.upload = Upload.upload({
method: 'POST',
url: 'http://localhost:8000/stores/add/',
data: {store : $scope.addForm, files: files},
});
files.upload.then(function (res) {
$timeout(function () {
handlerService.isValid(res.data);
if(res.data.isValid == true) {
$scope.showForm = false;
$scope.addForm = {};
}
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
// Math.min is to fix IE which reports 200% sometimes
files.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
}])
后端部分:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class StoresController extends Controller
{
public function add(Request $request) {
$files = array();
foreach($_FILES as $file) {
$filename = $file['name'];
$files[] = $file['name'];
$destination = '../public/assets/img/stores/' . $filename;
move_uploaded_file($file['tmp_name'] , $destination );
}
$store = $request->store;
$store['images'] = json_encode($files);
$isValid = false;
if(isset($store['name']) && $store['name'] != '') {
DB::table('stores')->insert(
$store
);
$isValid = true;
$message = 'store is added.';
} else {
$message = json_encode($request->all());
$isValid = false;
}
$result = [
'isValid' => $isValid,
'message' => $message
];
return json_encode($result);
}
}
但是整个请求是空的。没有文件上传部分,我可以毫无问题地添加商店。当我检查我的 AJAX 请求时,我还可以看到负载与商店和图像一起发送。我做错了什么?
最佳答案
我需要提供 resumeSizeUrl 或 resumeSize,以检查文件的进度。在我这样做之后,我的文件发送没有问题。
后端部分也不对。为了上传文件,我使用了 laravel 的 Request 类。这是当前的后端代码:
public function add(Request $request) {
$isValid = true;
DB::beginTransaction();
$store = $request->store;
if(isset($store['name']) && $store['name'] != '') {
$id = DB::table('stores')->insertGetId(
$store
);
} else {
$message = 'Naam mag niet leeg zijn';
$isValid = false;
}
if($isValid === true) {
// getting all of the post data
$files = $request->file('files');
// Making counting of uploaded images
$path = 'stores'.'/'.$id;
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
$rules = array('file' => 'mimes:png,gif,jpeg'); //'required|txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()) {
$destinationPath = 'img/'.$path.'/';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
} else {
$message = 'Alleen afbeeldingen zijn toegestaan';
$isValid = false;
}
}
if($isValid == true) {
if($uploadcount != $file_count) {
$message = 'Niet alle bestanden zijn geupload';
$isValid = false;
}
}
}
if($isValid == true) {
DB::commit();
$message = 'Store Toegevoegd!';
}
if($isValid == false) {
DB::rollBack();
}
$result = [
'isValid' => $isValid,
'message' => $message
];
return json_encode($result);
}
关于php - 请求为空 laravel ajax 多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015738/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送
我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)
在我的路线文件中我有:match'graphs/(:id(/:action))'=>'graphs#(:action)'如果是GET请求(工作)或POST请求(不工作),我想匹配它我知道我可以使用以下方法在资源中声明POST请求:post'/'=>:show,:on=>:member但是我怎样才能为比赛做到这一点呢?谢谢。 最佳答案 如果你同时想要POST和GETmatch'graphs/(:id(/:action))'=>'graphs#(:action)',:via=>[:get,:post]编辑默认值可以设置如下match'g
我正在尝试找出一种方法来显示来自不在RAILS_ROOT下(在RedHat或Ubuntu环境中)的已安装文件系统的图像。我不想使用符号链接(symboliclink),因为这个应用程序实际上是通过Tomcat部署的,而当我关闭Tomcat时,Tomcat会尝试跟随符号链接(symboliclink)并删除挂载中的所有图像。由于这些文件的数量和大小,将图像放在public/images下也不是一种选择。我查看了send_file,但它只会显示一张图片。我需要在一个格式良好的页面中显示6个请求的图像。由于膨胀,我宁愿不使用Base64编码,但我不知道如何将图像数据与呈现的页面一起传递下去。
我试图像这样在我的测试用例中执行获取:request.env['CONTENT_TYPE']='application/json'get:index,:application_name=>"Heka"虽然,它失败了:ActionView::MissingTemplate:Missingtemplatealarm_events/indexwith{:handlers=>[:builder,:haml,:erb,:rjs,:rhtml,:rxml],:locale=>[:en,:en],:formats=>[:html]尽管在我的Controller中我有:respond_to:html,
技术选型1,前端小程序原生MINA框架cssJavaScriptWxml2,管理后台云开发Cms内容管理系统web网页3,数据后台小程序云开发云函数云开发数据库(基于MongoDB)云存储4,人脸识别算法基于百度智能云实现人脸识别一,用户端效果图预览老规矩我们先来看效果图,如果效果图符合你的需求,就继续往下看,如果不符合你的需求,可以跳过。1-1,登录注册页可以看到登录页有注册入口,注册页如下我们的注册,需要管理员审核,审核通过后才可以正常登录使用小程序1-2,个人中心页登录成功以后,我们会进入个人中心页我们在个人中心页可以注册人脸,因为我们做人脸识别签到,需要先注册人脸才可以进行人脸比对,进
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它