Provider hosted asp.net mvc app upload file

190 views Asked by At

I am trying to create a simple post in which I am also attaching an image using Provider Hosted App on Sharepoint online, and I am not able to get SPHostUrl at Controller in HttpContext.Request, I mean SPHostUrl is missing in HttpContext.Request.

 [HttpPost]
    public ActionResult Contact(SimplePostModel model,HttpPostedFileBase file)
    {}

View

@using (Html.BeginForm("Contact","Home",null,FormMethod.Post,new { enctype= "multipart/form-data"}))

The question is if I am sending this part new { enctype= "multipart/form-data"} in the above mentioned statement of View, I am not able to get SPHostUrl parameter in HttpContext.Request.

If I am not sending the html attributes then I am able to get SPHostUrl parameter in HttpContext.Request. and without html attributes I am also not able to upload a file.

Thanks in Advance for your help.

1

There are 1 answers

0
Wasif On

I have solved the issue,by changing some piece of code on in the Razor View.

@using (Html.BeginForm("Contact","Home",null,FormMethod.Post,new { enctype= "multipart/form-data"}))

Instead of null for object route values I send the spHosturl like below.

@using (Html.BeginForm("Contact","Home",new { SPHostUrl = spUrl },FormMethod.Post,new {  enctype = "multipart/form-data" }))

the spUrl value comes like below

@{
var spUrl = Request.QueryString["SPHostUrl"];

}

I hope this will help for new sharepoint online developers