gpt4 book ai didi

c# - 在Azure函数中将Post文件转换为Pdf

转载 作者:行者123 更新时间:2023-12-03 02:57:03 27 4
gpt4 key购买 nike

我想要实现的是将包含文本和文件的数据从 html 页面发布到 azure 函数,然后将该数据发送到管理电子邮件。我已经尝试过,但现在能够获取发布的文件。

HTML 页面:

          <script>

function myFormOnSubmit()
{
var formData = new FormData();

formData.append("firstname",$('#firstname').val());
formData.append("lastname",$('#lastname').val());
formData.append("phonenumber",$('#phonenumber').val());
formData.append("email",$('#email').val());
formData.append("position",'Software Engineer');
formData.append("resume", $('input[type=file]')[0].files[0]);

$.ajax({
url: "function URL",
type: 'POST',
data: formData,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});

return false;
}

</script>

Azure 函数

      public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "options", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
if (req.Content.IsFormData())
log.Info("form data");
if(req.Content.IsMimeMultipartContent())
log.Info("multimedia content");
var provider = new MultipartFormDataStreamProvider("TempData\\");
var multimediaProvider = await req.Content.ReadAsMultipartAsync(provider);
var formData = multimediaProvider.FormData;


var apiKey = "SendGridApi Key";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e48a8bbb968194889da4808b89858d8aca878b89" rel="noreferrer noopener nofollow">[email protected]</a>", "Auto Generated");
var subject = "Application for " + formData["position"];




var to = new EmailAddress("admin email", "Admin");
var plainTextContent = subject;
var htmlContent = $"<strong>First Name :</strong> {formData["firstname"]}<br><strong>Last Name :</strong> {formData["lastname"]}<br><strong>Phone Number :</strong> {formData["phonenumber"]}<br><strong>Email :</strong> {formData["email"]}";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
client.SendEmailAsync(msg);
// parse query parameter

var resumeContent = multimediaProvider.Contents?.FirstOrDefault();
if (resumeContent!=null)
{
var resume = await resumeContent.ReadAsByteArrayAsync();
var file = Convert.ToBase64String(resume);
msg.AddAttachment("test.pdf", file);
}

return req.CreateResponse(HttpStatusCode.OK);
}

但我不知道如何从请求中获取文件并将其发送到电子邮件?

最佳答案

所以我想要实现的是,在将文件和表单数据发布到azure函数时获取该文件和数据,并通过azure函数将其发送到管理电子邮件,而不使用azure blob或其他存储。这是我使用 azure 临时存储作为路径的解决方案。

 public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "options", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
if (!req.Content.IsMimeMultipartContent())
{
return req.CreateErrorResponse(HttpStatusCode.BadRequest,new Exception("UnSupported Meida Type"));
}

var provider = new MultipartFormDataStreamProvider(Path.GetTempPath());

try
{
// Read the form data.
await req.Content.ReadAsMultipartAsync(provider);


}
catch (System.Exception e)
{
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}

var formData = provider.FormData;

var apiKey = "apiEkey";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234d4c7c5146534f5a63474c4e424a4d0d404c4e" rel="noreferrer noopener nofollow">[email protected]</a>", "Auto Generated");
var subject = "Application for " + formData["position"];
var to = new EmailAddress("toemail", "Admin");
var plainTextContent = subject;
var htmlContent = $"<strong>First Name :</strong> {formData["firstname"]}<br><strong>Last Name :</strong> {formData["lastname"]}<br><strong>Phone Number :</strong> {formData["phonenumber"]}<br><strong>Email :</strong> {formData["email"]}";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

foreach (MultipartFileData file in provider.FileData)
{
var bytes = File.ReadAllBytes(file.LocalFileName);
var fileBase64 = Convert.ToBase64String(bytes);
string fileName = file.Headers.ContentDisposition.FileName;
if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
{
fileName = fileName.Trim('"');
}
msg.AddAttachment(fileName, fileBase64);
}

client.SendEmailAsync(msg);




return req.CreateResponse(HttpStatusCode.OK);
}

关于c# - 在Azure函数中将Post文件转换为Pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138893/

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