Reputation: 143
I am stuck in upload file it works fine on local but when I deploy on server it show
500 internal server Error..
Here its my code please review..where m wrong?
Here's my controller code
[HttpPost]
public ActionResult GlovalValues(Global_values REC, HttpPostedFileBase uploadFile)
{
string strPath = "~/Laptop_File/";
string Server_path = "";
string GetFileName = "";
int id = REC.ID;
string Desc = REC.GS_Desc;
string Name = REC.GS_Name;
string values = REC.GS_Values;
int Effrows = 0;
if (uploadFile != null && uploadFile.ContentLength > 0)
{
try
{
if (!Directory.Exists(Server.MapPath(strPath)))
{
DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(strPath));
}
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
var filePath = Request.FilePath;
var FileExtension = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf('.') + 1).ToLower();
string ActualFileName = uploadFile.FileName;
GetFileName = Path.GetFileNameWithoutExtension(uploadFile.FileName);
if (file != null && file.ContentLength > 0)
{
string UploadFileName = Path.GetFileName(GetFileName + "." + FileExtension);
Server_path = Path.Combine(Server.MapPath("~/Laptop_File/"), UploadFileName);
if (FileExtension == "xlsx" || FileExtension == "xltx" || FileExtension == "xls" || FileExtension == "xlt ")
{
file.SaveAs(Server_path);
}
}
}
}
catch (Exception ex)
{
throw;
}
}
return RedirectToAction("Global_Values_List", "GS_Global_Values");
}
here's my view
@using (Html.BeginForm("GlovalValues", "GS_Global_Values", FormMethod.Post, new { @id = "id", @enctype = "multipart/form-data" }))
{
<div class="form-group">
<label> Uplaod File : </label>
<div class="col-md-10">
<input type="file" name="uploadFile" id="fileupload" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
Please help I cant figure out whats wrong
Upvotes: 2
Views: 3513
Reputation: 469
In my case it reason from permission.When I ran my project locally on IIS Express everything was fine,and I could upload files correctly, but when I deployed it on IIS 8,it had returned me "Error 500 Internal server Error" for file uploading. I have changed the permissions for "IIS_IUSRS" on "upload" folder in IIS 8 as follows:
now it is working fine.
Upvotes: 4
Reputation: 3540
Check on server that your application pool has rights of network services or other user with higher authority to Modify the directory & file writing. If your code is working in local this seems the only issue at moments.
Upvotes: 0