Receive, send file over Web Api from Mvc project to webapi using json. I have 2 different project 1 is Mvc and another one is webapi project. In mvc project here i choose the file.
- [HttpPost]
- public async Task<ActionResult> TestFileUpload(HttpPostedFileBase file)
- {
- string fName = "";
- string _documentname = String.Empty;
-
- try
- {
- foreach (string fileName in Request.Files)
- {
- var files = Request.Files[fileName];
-
- fName = file.FileName;
- if (file != null && file.ContentLength > 0)
- {
-
- var client = new HttpClient();
- string _url = _urlApi + "/test/document";
- var response = await client.PostAsJsonAsync(_url,"");
- var result = response.Content.ReadAsStringAsync().Result;
-
- }
-
- }
-
- }
- catch (Exception ex)
- {
-
- }
-
-
-
-
- string ErrorMsg = String.Empty;
- return Json(true, JsonRequestBehavior.AllowGet);
- }
so how can i send Request.Files
to webapi controller.??
Here is the webapi controller to save the file through blobstroage.
- [HttpPost]
- [Route("test/document")]
- public HttpResponseMessage testdocument()
- {
-
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
-
-
- CloudBlobContainer container = blobClient.GetContainerReference("Members");
-
-
- container.CreateIfNotExists();
- container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
-
-
-
- CloudBlockBlob blockBlob = container.GetBlockBlobReference(Doc.UserCode);
-
-
-
- blockBlob.Properties.ContentType = Doc.fileStream.ContentType;
- blockBlob.UploadFromStreamAsync(Doc.fileStream.InputStream);
-
-
-
- string imageFullPath = blockBlob.Uri.ToString();
-
-
-
- return this.Request.CreateResponse(HttpStatusCode.OK, true);
-
- return this.Request.CreateResponse(HttpStatusCode.OK, true);
- }
So how can i send the file in webapi??