我已经创建了函数应用程序来上传FTP服务器上的多个文件。我已经收到了使用req.Form.Files
的所有文件。但当我收到请求。实际上,我在HttpClient
请求中在req.Body
中找到了我的文件。当我从Body->FormData上传文件时,工作正常,但是现在我需要通过代码发送带有文件的post请求。
我在下面的代码中引用了Sending a Post using HttpClient and the Server is seeing an empty Json file这个链接。
HttpContent content = new StreamContent (stream);
content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
HttpResponseMessage response = client.PostAsync ("url", content).Result;
但是我想要req.Form.Files
中的文件。用户可能上传了多个文件或一个文件。
注意:现在我有一个由代码生成的文件。但是它不应该保存在本地,所以我试图发送流。在HttpContent
中
发布于 2022-11-15 11:24:14
下面是使用函数应用程序异步后的步骤
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
string Connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
string containerName = Environment.GetEnvironmentVariable("ContainerName");
var file = req.Form.Files["File"];**
var filecontent = file.OpenReadStream();
var blobClient = new BlobContainerClient(Connection, containerName);
var blob = blobClient.GetBlobClient(file.FileName);
byte[] byteArray = Encoding.UTF8.GetBytes(filecontent.ToString());
使用req.Form.Files
时的文件详细信息
成功地将文本文件上载到blob存储区。
编辑
下面是使用HttpClient的Post请求
var file = @"C:\Users\...\Documents\test.txt";
await using var stream = System.IO.File.OpenRead(file);
using var request = new HttpRequestMessage(HttpMethod.Post, "file");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream),"File", "test.txt" }
};
request.Content = content;
await client.SendAsync(request);
return new OkObjectResult("File uploaded successfully");
https://stackoverflow.com/questions/74384841
复制相似问题