Skip to content

Commit

Permalink
[Added] added ProfilePicture model and view
Browse files Browse the repository at this point in the history
  • Loading branch information
nobir committed Jul 20, 2022
1 parent e0af711 commit 66198df
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Models/ProfilePicture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace ERPSystemTimologio.Models
{
public class ProfilePicture
{
public string ErrorMessage { get; set; }
public decimal Filesize { get; set; }
public string Upload(HttpPostedFileBase file, string username)
{
try
{
var supportFileType = new[] { "jpg", "jpeg", "png", "gif" };
var getFileExtension = Path.GetExtension(file.FileName).Substring(1);

if (!supportFileType.Contains(getFileExtension))
{
ErrorMessage = "File must be jpg/jpeg/png/gif format";
return ErrorMessage;
}
else if (file.ContentLength > (Filesize * 1024))
{
ErrorMessage = "File size must be less than " + Filesize + "KB";
return ErrorMessage;
}

string _path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/images/avatar"), username + "." + getFileExtension);
file.SaveAs(_path);

return null;
}
catch (Exception)
{
ErrorMessage = "Upload Container Should Not Be Empty";
return ErrorMessage;
}
}
}
}
40 changes: 40 additions & 0 deletions Views/Dashboard/ProfilePicture.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

@{
ViewBag.Title = "ProfilePicture";

ERPSystemTimologio.EF.User sessionUser = null;

if (Session["user"] != null)
{
sessionUser = (ERPSystemTimologio.EF.User)Session["user"];
}
}

<form action="@Url.Action("ProfilePicture", "Dashboard")" method="POST" enctype="multipart/form-data">
<div class="card text-center">
<div class="card-body">
<div class="row mb-3 has-validation">
<label for="avatar" class="col-sm-3 col-form-label">Avatar</label>
<div class="col-sm-9">
<div class="custom-file">
<input type="file"
class="custom-file-input form-control @(!ViewData.ModelState.IsValidField("Avatar") ? "is-invalid" : "")" id="avatar"
name="Avatar">
<label class="custom-file-label" for="avatar">Choose file</label>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-sm-9 offset-sm-3">
<button id="avatar-upload-btn" type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</div>
@if (sessionUser != null && sessionUser.Avatar != null)
{
<div class="card-footer">
<img src="@Url.Content("~/Content/images/"+sessionUser.Avatar.ToString())" class="img-thumbnail rounded mx-auto d-block" alt="@sessionUser.Name">
</div>
}
</div>
</form>

0 comments on commit 66198df

Please sign in to comment.