我希望从数据库中的表单AngularJS保存数据。我有字段的形式。但我想另外发送带有数据的文件。数据发送没有问题。但我不知道如何发送文件与数据。我必须发两个请求吗?如何将文件与数据一起发送?
这是我的输入文件。
<div class="pure-control-group">
<label for="code">UID</label>
<input id="uid" ng-model="form.uid" type="text" placeholder="UID" required="" disabled="">
</div>
<div class="pure-control-group">
<label for="code">Code</label>
<input id="code" ng-model="form.code" type="text" placeholder="Code" required="">
</div>
<div class="pure-control-group">
<label for="name">Name</label>
<input id="name" ng-model="form.name" type="text" placeholder="Name" required="" autocomplete="new-password">
</div>
<div class="pure-control-group">
<label for="town">Town</label>
<input id="town" ng-model="form.town" type="text" placeholder="Town" required="" autocomplete="new-password">
</div>
<div class="pure-control-group">
<label for="date_start">Date Start</label>
<input id="date_start" ng-model="form.date_start" type="date" placeholder="Password" autocomplete="new-password">
</div>
<div class="pure-control-group">
<label for="date_stop">Date Stop</label>
<input id="date_stop" ng-model="form.date_stop" type="date" placeholder="Password" autocomplete="new-password">
</div>
<div class="pure-control-group">
<label>Status</label>
<select class="form-control select" ng-model="form.status">
<option ng-repeat="status in statuses" value="{{status.name}}">{{status.name}}</option>
</select>
</div>
<div class="pure-control-group">
<label for="logo">Logo</label>
<input id="logo" type="file" ng-model="form.logo" name="file" accept="image/*" ngf-max-size="5MB" required>
</div>
Angularjs控制器:
$scope.submit = function (data)
{
$fetch.post('api/admin/places', data)
.then(function (response)
{
if (response.success)
{
Notification.success(response.message);
$state.go('app.admin.places');
} else
{
var msg = response && response.message ? response.message : 'There was an error in data update. Please contact your administrator';
Notification.error(msg);
}
})
.finally(function () {
$loader.hide();
});
}
};
Laravel控制器:
public function add(Request $request) {
try {
Place::create([
'uid' => Uuid::generate()->string,
'code' => $request->input('code'),
'name' => $request->input('name'),
'town' => $request->input('town'),
'date_start' => $request->input('date_start'),
'date_stop' => $request->input('date_stop'),
'status' => $request->input('status'),
'sort' => 1,
'position' => $request->input('position'),
]);
} catch (\Exception $ex) {
}
return ['success' => true, 'message' => 'Place was successfully added'];
}
那么,如何将文件与数据一起保存?
答案 0 :(得分:0)
使用以下代码通过$ post将文件上传到服务器: -
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.WindowConstants;
public class CircleList extends JFrame implements ActionListener {
JButton addCircle;
int nCircle = 1;
int step = 0;
int shift = 0;
int d = 0;
public static void main (String [] args){
new CircleList();
}
private class Circle {
public int x1;
public int y1;
public int w;
public int h;
public Color color;
}
private List<Circle> whiteList = new ArrayList<>();
private List<Circle> blackList = new ArrayList<>();
public CircleList() {
super("CircleList");
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.gray);
addCircle = new JButton("Add Circle");
add(addCircle,BorderLayout.SOUTH);
addCircle.addActionListener(this);
repaint();
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.fillOval(35, 35, 35, 35);
if (d==1) {
for (Circle s: whiteList ) {
paintWhiteCircle(g, s);
}
addWhiteCircle();
}
else if (d==2) {
for (Circle s: blackList ) {
paintBlackCircle(g, s);
}
addBlackCircle();
}
}
public void paintWhiteCircle(Graphics g, Circle circle) {
g.setColor(circle.color);
g.fillOval(circle.x1, circle.y1, circle.w, circle.h);
}
public void paintBlackCircle(Graphics g, Circle circle) {
g.setColor(circle.color);
g.fillOval(circle.x1, circle.y1, circle.w, circle.h);
}
public void addBlackCircle(){
Circle circle = new Circle();
circle.x1 = 40 + shift;
circle.y1 = 30 ;
circle.w = 30 + step;
circle.h = 30 + step;
circle.color = new Color(0,0,0);
this.blackList.add(circle);
}
public void addWhiteCircle() {
Circle circle = new Circle();
circle.x1 = 100 + shift;
circle.y1 = 30 ;
circle.w = 50 + step;
circle.h = 50 + step;
circle.color = new Color(255*65536+255*256+255);
this.whiteList.add(circle);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
d = 0;
if (source == addCircle) {
for (nCircle = 2; nCircle <= 5; nCircle++) {
if (nCircle == 2) {
d = 1;
step+=20;
shift += 20;
nCircle++;
}
else if (nCircle == 3) {
d = 2;
step+=20;
shift += 20;
}
else if (nCircle == 4) {
d=1;
step+=20;
shift += 20;
}
else if (nCircle == 5)
repaint(); // it should clear the window and start with one circle like the beginning
}
repaint();
}
}
}
希望你知道剩下的部分。