Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 128.5k

Receive, send file over Web Api from Mvc project to webapi u

May 20 2017 10:26 AM
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.
 
  1. [HttpPost]  
  2.    public async Task<ActionResult> TestFileUpload(HttpPostedFileBase file)  
  3.    {  
  4.        string fName = "";  
  5.        string _documentname = String.Empty;  
  6.   
  7.        try  
  8.        {  
  9.            foreach (string fileName in Request.Files)  
  10.            {  
  11.                var files = Request.Files[fileName];  
  12.                //Save file content goes here  
  13.                fName = file.FileName;  
  14.                if (file != null && file.ContentLength > 0)  
  15.                {  
  16.   
  17.                    var client = new HttpClient();  
  18.                    string _url = _urlApi + "/test/document";  
  19.                    var response = await client.PostAsJsonAsync(_url,"");  
  20.                    var result = response.Content.ReadAsStringAsync().Result;  
  21.   
  22.                }  
  23.   
  24.            }  
  25.   
  26.        }  
  27.        catch (Exception ex)  
  28.        {  
  29.   
  30.        }  
  31.   
  32.   
  33.   
  34.        //var _userin = JsonConvert.DeserializeObject();  
  35.        string ErrorMsg = String.Empty;  
  36.        return Json(true, JsonRequestBehavior.AllowGet);  
  37.    }  
 

so how can i send Request.Files to webapi controller.??

Here is the webapi controller to save the file through blobstroage.

 
  1. [HttpPost]  
  2.     [Route("test/document")]  
  3.     public HttpResponseMessage testdocument()  
  4.     {  
  5.         //Save document into Blob storage  
  6.         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  7.   
  8.         // Retrieve a reference to a container.  
  9.         CloudBlobContainer container = blobClient.GetContainerReference("Members");  
  10.   
  11.         // Create the container if it doesn't already exist.  
  12.         container.CreateIfNotExists();  
  13.         container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });  
  14.   
  15.   
  16.         // Retrieve reference to a blob named "myblob".  
  17.         CloudBlockBlob blockBlob = container.GetBlockBlobReference(Doc.UserCode);  
  18.   
  19.         // Create or overwrite the "myblob" blob with contents from a local file.  
  20.   
  21.         blockBlob.Properties.ContentType = Doc.fileStream.ContentType;  
  22.         blockBlob.UploadFromStreamAsync(Doc.fileStream.InputStream);  
  23.   
  24.         //blockBlob.UploadFromStream(Doc.fileStream);  
  25.         // blockBlob.s  
  26.         string imageFullPath = blockBlob.Uri.ToString();  
  27.   
  28.   
  29.         //Return New UserID  
  30.         return this.Request.CreateResponse(HttpStatusCode.OK, true);  
  31.   
  32.         return this.Request.CreateResponse(HttpStatusCode.OK, true);  
  33.     }  
 So how can i send the file in webapi??

Answers (1)