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
TestFileUpload(HttpPostedFileBase file) - {
- string fName = "";
- string _documentname = String.Empty;
- try
- {
- foreach (string fileName in Request.Files)
- {
- var files = Request.Files[fileName];
- //Save file content goes here
- 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)
- {
- }
- //var _userin = JsonConvert.DeserializeObject();
- 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()
- {
- //Save document into Blob storage
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- // Retrieve a reference to a container.
- CloudBlobContainer container = blobClient.GetContainerReference("Members");
- // Create the container if it doesn't already exist.
- container.CreateIfNotExists();
- container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
- // Retrieve reference to a blob named "myblob".
- CloudBlockBlob blockBlob = container.GetBlockBlobReference(Doc.UserCode);
- // Create or overwrite the "myblob" blob with contents from a local file.
- blockBlob.Properties.ContentType = Doc.fileStream.ContentType;
- blockBlob.UploadFromStreamAsync(Doc.fileStream.InputStream);
- //blockBlob.UploadFromStream(Doc.fileStream);
- // blockBlob.s
- string imageFullPath = blockBlob.Uri.ToString();
- //Return New UserID
- return this.Request.CreateResponse(HttpStatusCode.OK, true);
- return this.Request.CreateResponse(HttpStatusCode.OK, true);
- }
Manas MohapatraPosted May 21, 2017, 5:55 AM