我使用了之前发现的一些代码,一次上传1张图片,这非常有效。我想使用相同的代码风格,但是对于多个图像。
模特:
<div class="form-group">
<label class="col-sm-2 control-label" for="CoverFoto">picture</label>
<div class="col-sm-5">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">select_an_image</span><span class="fileinput-exists">change</span><input type="file" name="userfile"></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput"><?php echo $this->lang->line("remove"); ?></a>
</div>
</div>
</div>
</div>
视图(只是关于图像的部分):
if (!empty($_FILES['userfile']['name'])) {
$gamma->CoverFoto = $this->gamma_model->cover_upload(url_title($gamma->Naam, '_', true));
}
控制器:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
public class TraverseDisabled extends JFrame {
public static final String VALUE_NOT_SET_MESSAGE = "Click to set this value.";
public static final String VALUE_SET_DEFAULT_MESSAGE = "Edit value or use CTRL + click to unset this value.";
public static final String VALUE_NOT_SET_DEFAULT_VALUE = "<not-set>";
private JPanel panel;
private JTextField textfield1;
private JCheckBox checkbox1;
private JTextArea textarea1;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JButton button;
// associate labels with components
final private Map<JComponent, JLabel> bindings = new HashMap<JComponent, JLabel>();
public TraverseDisabled() {
setTitle("Form");
initComponents();
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initComponents() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
setLayout(new BorderLayout());
add(panel);
GridBagConstraints gbc;
label1 = new JLabel("Field1:");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
panel.add(label1, gbc);
textfield1 = new JTextField(20);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(textfield1, gbc);
textfield1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
onJComponentClicked(e, textfield1, null);
}
});
bindings.put(textfield1, label1);
enableEditingForJTextComponent(textfield1, true, null);
label2 = new JLabel("Checkbox1:");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
panel.add(label2, gbc);
checkbox1 = new JCheckBox();
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
panel.add(checkbox1, gbc);
checkbox1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
onJComponentClicked(e, checkbox1, false);
}
});
bindings.put(checkbox1, label2);
enableEditingForJCheckBoxComponent(checkbox1, false, false);
label3 = new JLabel("Area1:");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.NORTHWEST;
panel.add(label3, gbc);
textarea1 = new JTextArea(5, 20);
JScrollPane scrollPane1 = new JScrollPane(textarea1);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(scrollPane1, gbc);
textarea1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
onJComponentClicked(e, textarea1, null);
}
});
bindings.put(textarea1, label3);
enableEditingForJTextComponent(textarea1, false, VALUE_NOT_SET_DEFAULT_VALUE);
button = new JButton("Apply");
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.EAST;
panel.add(button, gbc);
// react to label focus
FocusListener labelFocusListener = new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
JLabel label = (JLabel) e.getSource();
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
}
@Override
public void focusLost(FocusEvent e) {
JLabel label = (JLabel) e.getSource();
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, label.getBackground()));
}
};
// action for unsetting values
Action unsetAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JComponent comp = (JComponent) e.getSource();
MouseEvent evt = new MouseEvent(
comp, MouseEvent.MOUSE_CLICKED,
1, MouseEvent.CTRL_DOWN_MASK, 0, 0, 1, false);
onJComponentClicked(evt, comp, comp instanceof JCheckBox ? false : null);
}
};
// action for setting values
Action setAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
for (Map.Entry<JComponent, JLabel> entry : bindings.entrySet()) {
if (e.getSource() == entry.getValue()) {
MouseEvent evt = new MouseEvent(
entry.getKey(), MouseEvent.MOUSE_CLICKED,
1, 0, 0, 0, 1, false);
onJComponentClicked(evt, entry.getKey(), entry.getKey() instanceof JCheckBox ? false : null);
break;
}
}
}
};
// initialize them labels
for (JLabel label : bindings.values()) {
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, label1.getBackground()));
label.addFocusListener(labelFocusListener);
InputMap inputMap = label.getInputMap();
ActionMap actionMap = label.getActionMap();
KeyStroke key;
key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(key, "set-value");
actionMap.put("set-value", setAction);
}
// make it possible to unset from focused component
for (JComponent comp : bindings.keySet()) {
InputMap inputMap = comp.getInputMap();
ActionMap actionMap = comp.getActionMap();
KeyStroke key;
key = KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_DOWN_MASK);
inputMap.put(key, "unset-value");
actionMap.put("unset-value", unsetAction);
}
}
private Object onJComponentClicked(java.awt.event.MouseEvent evt, JComponent component, Object previousValue) {
if (!component.isEnabled()) {
if (component instanceof JCheckBox) {
enableEditingForJCheckBoxComponent((JCheckBox)component, true, (Boolean)previousValue);
} else if (component instanceof JTextComponent) {
enableEditingForJTextComponent((JTextComponent)component, true, (String)previousValue);
}
evt.consume();
} else if (evt.isControlDown()) {
if (component instanceof JCheckBox) {
JCheckBox cb = (JCheckBox)component;
previousValue = !cb.isSelected();
enableEditingForJCheckBoxComponent((JCheckBox) component, false, (Boolean)previousValue);
} else if (component instanceof JTextComponent) {
previousValue = ((JTextComponent)component).getText();
enableEditingForJTextComponent((JTextComponent)component, false, VALUE_NOT_SET_DEFAULT_VALUE);
}
evt.consume();
}
return previousValue;
}
private void enableEditingForJTextComponent(JTextComponent textComponent, boolean enable, String text) {
if (!enable) {
textComponent.setEnabled(false);
textComponent.setText(text);
textComponent.setToolTipText(VALUE_NOT_SET_MESSAGE);
} else {
textComponent.setEnabled(true);
textComponent.setText(text);
textComponent.setToolTipText(VALUE_SET_DEFAULT_MESSAGE);
textComponent.requestFocusInWindow();
}
bindings.get(textComponent).setFocusable(!enable); // change focusable
}
private void enableEditingForJCheckBoxComponent(JCheckBox checkBox, boolean enable, boolean value) {
if (!enable) {
checkBox.setEnabled(false);
checkBox.setSelected(value);
checkBox.setToolTipText(VALUE_NOT_SET_MESSAGE);
} else {
checkBox.setEnabled(true);
checkBox.setSelected(value);
checkBox.setToolTipText(VALUE_SET_DEFAULT_MESSAGE);
}
bindings.get(checkBox).setFocusable(!enable); // change focusable
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TraverseDisabled().setVisible(true);
}
});
}
}
有没有办法使用这个代码,所以我可以上传多个图像?
答案 0 :(得分:2)
查看此代码可能有助于您了解如何处理多个图像
function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
代码中发生了什么?
$ _ FILE ----&gt;它是通过POST方法上传到当前脚本的关联项目数组。进一步查看此LINK
它是一个在脚本的所有范围内可用的自动变量
<hook>
<portal-properties>portal.properties</portal-properties>
<language-properties>content/Tips.properties</language-properties>
<servlet-filter>
<servlet-filter-name>MyFilter</servlet-filter-name>
<servlet-filter-impl>com.ncs.phm.ws.PhmWebServiceFilter</servlet-filter-impl>
</servlet-filter>
<servlet-filter-mapping>
<servlet-filter-name>MyFilter</servlet-filter-name>
<url-pattern>/delegate/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</servlet-filter-mapping>
并且在你的HTML标记中不要忘记:
输入元素必须有多个=&#34;多个&#34;或只是多个
3 $这 - &GT;负载&GT;库(&#39;上传&#39); //加载库
4.回调,$ this-&gt; upload-&gt; do_upload()会将在给定字段名称中选择的文件上传到目标文件夹。
5.回调$ this-&gt; upload-&gt; data()返回与上传文件相关的数据数组,如文件名,路径,大小等。