gpt4 book ai didi

java - 发送带有附件的邮件 - angularJS + Java Spring

转载 作者:行者123 更新时间:2023-12-02 11:50:14 26 4
gpt4 key购买 nike

我有可用的电子邮件发送器,但没有附件。我尝试过这样的事情:

HTML 中的简单输入:

<input type="file" simple-change="uploadFiles($event)"/>

JS 文件选择器:

    $scope.uploadFiles = function (event) {
$scope.file= event.target.files[0];
};

和 JS 电子邮件发送器:

$scope.sendEmail = function(){
$scope.emailData = new EmailData();
$scope.emailData.to = "example@gmail.com";
$scope.emailData.from = "boss@gmail.com";
$scope.emailData.subject = "Errors";
$scope.emailData.title = $scope.topic;
$scope.emailData.description = $scope.description;
$scope.emailData.template = "templateErrors";

$http.post("sendemail/attachment", $scope.emailData, $scope.file, {headers: {'Content-Type': undefined} })
.then(function (response) {
$scope.succes = true;
},
function(fail) {
$scope.error = true;
});
}

Java Controller :

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestBody EmailData emailData, CommonsMultipartFile file) {
return emailSenderAttachment.sendDataAttach(emailData, file);
}

但仍然出现错误 500: ""message":"org/apache/commons/fileupload/FileUploadException""

请帮忙:(

最佳答案

尝试下面的代码使用 $upload.upload 而不是 $http.post 将文件上传到服务器。

AngularJs

$upload.upload({
method : 'POST',
url : 'sendemail/attachment',
file : $scope.file,
data : $scope.emailData
}).success(function(response) {
console.log("Success");
}).error(function(error) {
console.log("Error");
});

Spring Controller

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestParam("file") MultipartFile file, EmailData emailData) {
return emailSenderAttachment.sendDataAttach(emailData, file);
}

更多详情可以引用我这篇文章的回答Multipart File Upload using AngularJS and SpringMVC

关于java - 发送带有附件的邮件 - angularJS + Java Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com