I want to send a file to a web api. Does this work or do I have to use an MVC controller instead and use Request.Files?
I have a simple file upload like this
<div class="form-group">
<label class="control-label">Upload Custom Clause</label>
<input id="fileupload" type="file" onchange="FileSelected(searchForm.fileupload)" name="fileupload" />
</div>
My form is marked as
enctype="multipart/form-data"
I then get the file from the uploader and assign it to a property on an object I am not using the javascript file reader, can the actual file not map to HttpPostedFileBase in c#?
function FileSelected(fileuploader: any) {
var reader = new FileReader();
var file = fileuploader.files[0];
uploadedFile = file
//reader.onload = function (evt: any) {
// filecontentStr = evt.target.result;
//}
}
I then have a c# web api method which maps an object. The file is a property in quoteData.UploadedFile and is of type HttpPostedFileBase.
[Route("api/quoteEngine/createPolicy")]
[HttpPost]
public IHttpActionResult PostPolicy([FromBody] QuoteEngineView quoteData)
{
So I then have this, but UploadedFile is null... :(
if (policy.UploadedFile != null)
{
var temppath = $"{Directory.GetCurrentDirectory()}\\{policy.UploadedFile.FileName}";
//var file = File.Create(temppath);
Stream str = policy.UploadedFile.InputStream;
BinaryReader Br = new BinaryReader(str);
clausebuffer = Br.ReadBytes((Int32)str.Length);
string result = System.Text.Encoding.UTF8.GetString(clausebuffer);
File.WriteAllText(temppath, result);
}
Aucun commentaire:
Enregistrer un commentaire