我试图研究和理解文档,但无法解决我的问题。
我有一个允许我选择和上传多个文件的ModelForm。 但是,保存后,尽管有多个选择,但只有1个文件保存到我的媒体根文件夹中。
我的猜测是,getlist中所有文件的文件名都是相同的(因为它们会同时上传),它会以某种方式相互覆盖,最终只保存1个媒体。感谢社区的帮助,谢谢!
package com.viremp.component;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.time.LocalDate;
import javax.imageio.ImageIO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import com.viremp.core.domain.Screenshot;
import com.viremp.core.domain.User;
import com.viremp.core.repository.ScreenshotRepository;
import com.viremp.core.repository.UserRepository;
import com.viremp.core.repository.impl.ScreenshotRepositoryImpl;
import com.viremp.core.repository.impl.UserRepositoryImpl;
public class JobStoredScreenShot implements Job {
String email;
User user = new User();
public void setEmail(String Email) {
this.email = Email;
System.out.println("in job stored class email is: " + email);
user.setEmail(email);
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
SchedulerContext schedulerContext = arg0.getScheduler().getContext();
// Below line gets the value from context.
// Just get it and cast it in to correct type
String email = (String) schedulerContext.get("email");
System.out.println(email);
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
System.out.println("in execute " + user.getEmail() + ".................: " + email);
UserRepository userRepository = new UserRepositoryImpl();
user = userRepository.getUserByEmail(email);
System.out.println("USER IS: " + user.getUsername() + " id is : " + user.getId());
// byte[] buffer = (((DataBufferByte)
// (image).getRaster().getDataBuffer()).getData());
InputStream inputStream = new ByteArrayInputStream(imageInByte);
LocalDate localDate = LocalDate.now();
Screenshot screenshot = new Screenshot();
screenshot.setInput(inputStream);
screenshot.setScreenshotName(user.getUsername());
screenshot.setUser(new User());
screenshot.getUser().setId(user.getId());
screenshot.setScreenShotTime(java.sql.Date.valueOf(localDate));
System.out.println("id is " + 1l);
ScreenshotRepository screenshotRepository = new ScreenshotRepositoryImpl();
screenshotRepository.create(screenshot);
ImageIO.write(image, "png", new File("d:\\screenshot.png"));
System.out.println("screenshot taken");
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
forms.py
class FileFieldForm(forms.ModelForm):
stuff_image = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
class Meta:
model = Thing
fields = ('title', 'description', 'quantity','stuff_image')
-
model.py
def generate_filename(instance, filename):
ext = filename.split('.')[-1]
return '' + str(int(time())) + '.' + ext
class Thing(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(blank = True)
quantity = models.IntegerField(blank =True)
creation_date = models.DateTimeField(auto_now_add=True)
stuff_image = models.FileField(upload_to=generate_filename)
def __unicode__(self):
return self.title
class Meta:
ordering = ['title']
@receiver(post_delete, sender=Thing)
def stuff_post_delete_handler(sender, **kwargs):
Thing = kwargs['instance']
storage, path = Thing.stuff_image.storage, Thing.stuff_image.path
storage.delete(path)
答案 0 :(得分:0)
如果要将多个图像链接到Thing
模型,则需要使用Image
模型的ForeignKey创建Thing
模型。 FileField一次只能与一个文件相关。
答案 1 :(得分:0)
要在django中上传多个图像/文件,请使用django-multiupload包,请参阅(文档)[https://pypi.python.org/pypi/django-multiupload]或(GitHub Page)[https://github.com/Chive/django-multiupload]。只需按照说明和示例操作即可。工作完美无缺。