Я пытаюсь загрузить CSV-файл с локального диска в веб-API.
Sub Upload()
Dim objHTTP As New MSXML2.ServerXMLHTTP60
Dim url As String
Dim filename, resp As String
Dim boundary, payload As String
filename = "C:\temp\File2.csv" '& ";type=text/csv"
boundary = "-----------------"
url = "https://localhost:7102/api/v1/SThree/upload/bkm_volume"
objHTTP.Open "POST", url, False
objHTTP.setRequestHeader "accept", "*/*"
objHTTP.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
payload = boundary & vbCrLf & "Content-Disposition: form-data; file=" & Chr(34) & filename & Chr(34) & vbCrLf & boundary
Open filename For Input As #1
contents = Input$(LOF(1), #1)
Close #1
' Not sure if the contents need to be in the payload or if it's supposed to just be the filename
payload = payload & vbCrLf & contents & vbCrLf & boundary
objHTTP.setRequestHeader "Content-Length", LenB(payload)
objHTTP.send payload
If objHTTP.Status = "200" Then 'success
Else
resp = objHTTP.responseText
MsgBox resp
End If
End Sub
API:
public async Task UploadFile(IFormFile? file, string path)
{
if (file == null || file.Length == 0)
{
return BadRequest("No file provided.");
}
if (file.Length > Definitions.MaxFileSize)
{
return BadRequest("Invalid path. The specified file was too large.");
}
var outputDirectory = Path.Combine(Path.GetTempPath(), "UploadFiles");
var tempPath = Path.Combine(outputDirectory, file.FileName);
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
await using (var stream = new FileStream(tempPath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... -using-vba