I want to save Image and some other information to databse in my asp.net mc3 project. I've saved Image to database before and it worked. my code in my controller was this:
public ActionResult savetodb()
{
if (Request.Files.Count > 0 && Request.Files[0] != null)
{
HttpPostedFileBase file = Request.Files[0];
var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName);
file.SaveAs(path);
byte[] buffer = System.IO.File.ReadAllBytes(path);
myAd.AdImage = buffer;
StoreDb.AddToAds(myAd);
StoreDb.SaveChanges();
}
return View();
}
}
Now I changed the table and want to save other information more than Image to database. Now my code is like this:
public ActionResult savetodb(AdvertiseView model)
{
if (Request.Files.Count > 0 && Request.Files[0] != null)
{
HttpPostedFileBase file = Request.Files[0];
var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName);
file.SaveAs(path);
byte[] buffer = System.IO.File.ReadAllBytes(path);
myAd.AdImage = buffer;
}
myAd.AdTitle = model.AdTitle;
myAd.AdContext = model.context;
myAd.AdScope = model.Scope;
storedb.AddToAds(myAd);
storedb.SaveChanges();
return View();
}
there isn't any problem with other infos but image cant be saved. I understand that
Request.Files.Count
return 0. I don't know what should I do now. Can anybody help me please? Thanks alot.
I'd use a view model.
Let's suppose that you have a domain model first:
public class MyDomainModel
{
public byte[] AdImage { get; set; }
public string Description { get; set; }
}
then define a view model:
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
}
a controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// TODO: move this mapping logic into a
// mapping layer to avoid polluting the controller
// I would recommend AutoMapper for this purpose
// http://automapper.org/
using (var stream = new MemoryStream())
{
model.File.InputStream.CopyTo(stream);
var image = stream.ToArray();
var domainModel = new MyDomainModel
{
AdImage = image,
Description = model.Description
};
// TODO: persist the domain model by passing it to a method
// on your DAL layer
}
return Content("Thanks for submitting");
}
}
and once the recommended refactoring is complete:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
MyDomainModel domainModel = Mapper.Map<MyViewModel, MyDomainModel>(model);
// TODO: persist the domain model by passing it to a method
// on your DAL layer
return Content("Thanks for submitting");
}
}
and finally a view to allow the user upload the file:
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
#Html.LabelFor(x => x.Description)
#Html.EditorFor(x => x.Description)
</div>
<div>
#Html.LabelFor(x => x.File)
#Html.TextBoxFor(x => x.File, new { type = "file" })
#Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">OK</button>
}
Use HttpPostedFileBase as a parameter on the action.
Use this if you are sending only one file. If you are allowing multiple then you have to use IEnumerable<HttpPostedFileBase> files as the param.
public ActionResult savetodb(HttpPostedFileBase file)
{
if(file != null)
{
var path = Path.Combine(Server.MapPath("~/Content/Image"), file.FileName);
file.SaveAs(path);
byte[] buffer = System.IO.File.ReadAllBytes(path);
myAd.AdImage = buffer;
StoreDb.AddToAds(myAd);
StoreDb.SaveChanges();
}
return View();
}
You must also make sure your Form is properly built in your View
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" })) {
....
}
Also be aware that by default, the browser filesize upload limit is 4MB, if you want to upload anything larger than that you will need to configure your settings in the web.config file
Add a property to your view model to get this file:
public class AdvertiseView
{
...
public HttpPostedFileBase NameOfFileInput;
....
}
So you can grab the file as a property of the model:
if (myAd.NameOfFileInput != null)
{
var path = Path.Combine(Server.MapPath("~/Content/Image"), myAd.NameOfFileInput.FileName);
myAd.NameOfFileInput.SaveAs(path);
byte[] buffer = System.IO.File.ReadAllBytes(path);
myAd.AdImage = buffer;
}
Of course you can use the same property AdImage and just save it to the right place, without copying the buffer if its of the same type.
Related
I am trying to do CRUD operation using Kendo UI to show my data in a grid view.
I can read the data from my database with the following code in my controller (my tables are in SQL Server and connected through a connection string):
[Area("Admin")]
public class HeaderMenuController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HeaderMenuController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
return View();
}
public DataSourceResult Products_Read([DataSourceRequest] DataSourceRequest request)
{
return _unitOfWork.HeaderMenu.GetAll().ToDataSourceResult(request);
}
}
index.cshtml
#(Html.Kendo().Grid<MSDACE.Models.HeaderViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(product => product.HeaderMenuID).Width(100);
columns.Bound(product => product.HeaderMenuName);
columns.Bound(product => product.HeaderMenuDispOrder).Width(250);
columns.Command(commands =>
{
commands.Destroy(); // The "destroy" command removes data items.
}).Title("Commands").Width(200);
})
.ToolBar(toolbar =>
{
toolbar.Create(); // The "create" command adds new data items.
toolbar.Save(); // The "save" command saves the changed data items.
})
.Editable(editable => editable.Mode(GridEditMode.InCell)) // Use in-cell editing mode.
.DataSource(dataSource =>
dataSource.Ajax()
.Batch(true) // Enable batch updates.
.Model(model =>
{
model.Id(product => product.HeaderMenuID); // Specify the property which is the unique identifier of the model.
model.Field(product => product.HeaderMenuID).Editable(false); // Make the ProductID property not editable.
})
.Create(create => create.Action("Products_Create", "HeaderMenu")) // Action method invoked when the user saves a new data item.
.Read(read => read.Action("Products_Read", "HeaderMenu")) // Action method invoked when the Grid needs data.
.Update(update => update.Action("Products_Update", "HeaderMenu")) // Action method invoked when the user saves an updated data item.
.Destroy(destroy => destroy.Action("Products_Destroy", "HeaderMenu")) // Action method invoked when the user removes a data item.
)
.Pageable()
)
The problem is I can't the delete or create operations.
I have written the GetAll, Remove, Get and other functions, but my problem is to reflect in the Kendo Grid
IUnitOfWork.cs
public interface IUnitOfWork : IDisposable
{
IHeaderMenuRepository HeaderMenu { get; }
void Save();
}
IHeaderMenuRepository.cs
public class HeaderMenuRepository : Repository<HeaderMenu>, IHeaderMenuRepository
{
private readonly ApplicationDbContext _db;
public HeaderMenuRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
public IEnumerable<SelectListItem> GetHeaderMenuList()
{
return _db.HeaderMenu.Select(i => new SelectListItem()
{
Text = i.HeaderMenuName,
Value = i.HeaderMenuID.ToString()
});
}
public void Update(HeaderMenu headerMenu)
{
var objFrobDb = _db.HeaderMenu.FirstOrDefault(s => s.HeaderMenuID == headerMenu.HeaderMenuID);
objFrobDb.HeaderMenuName = headerMenu.HeaderMenuName;
objFrobDb.HeaderMenuDesc = headerMenu.HeaderMenuDesc;
objFrobDb.HeaderMenuActive = headerMenu.HeaderMenuActive;
objFrobDb.HeaderMenuDispOrder = headerMenu.HeaderMenuDispOrder;
_db.SaveChanges();
}
IRepository
public interface IRepository<T> where T : class
{
T Get(int id);
IEnumerable<T> GetAll(
Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = null
);
T GetFirstOrDefault(
Expression<Func<T, bool>> filter = null,
string includeProperties = null
);
void Add(T entity);
void Remove(int id);
void Remove(T entity);
}
UnitOfWork.cs
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _db;
public UnitOfWork(ApplicationDbContext db)
{
_db = db;
HeaderMenu = new HeaderMenuRepository(_db);
}
public IHeaderMenuRepository HeaderMenu { get; private set; }
public void Dispose()
{
_db.Dispose();
}
public void Save()
{
_db.SaveChanges();
}
}
Repository.cs
public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext Context;
internal DbSet<T> dbSet;
public Repository(DbContext context)
{
Context = context;
this.dbSet = context.Set<T>();
}
public void Add(T entity)
{
dbSet.Add(entity);
}
public T Get(int id)
{
return dbSet.Find(id);
}
public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
//include properties will be comma seperated
if (includeProperties != null)
{
foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
return query.ToList();
}
public T GetFirstOrDefault(Expression<Func<T, bool>> filter = null, string includeProperties = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
//include properties will be comma seperated
if (includeProperties != null)
{
foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
return query.FirstOrDefault();
}
public void Remove(int id)
{
T entityToRemove = dbSet.Find(id);
Remove(entityToRemove);
}
public void Remove(T entity)
{
dbSet.Remove(entity);
}
}
HeaderMenuRepository.cs
public class HeaderMenuRepository : Repository<HeaderMenu>, IHeaderMenuRepository
{
private readonly ApplicationDbContext _db;
public HeaderMenuRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
public IEnumerable<SelectListItem> GetHeaderMenuList()
{
return _db.HeaderMenu.Select(i => new SelectListItem()
{
Text = i.HeaderMenuName,
Value = i.HeaderMenuID.ToString()
});
}
public void Update(HeaderMenu headerMenu)
{
var objFrobDb = _db.HeaderMenu.FirstOrDefault(s => s.HeaderMenuID == headerMenu.HeaderMenuID);
objFrobDb.HeaderMenuName = headerMenu.HeaderMenuName;
objFrobDb.HeaderMenuDesc = headerMenu.HeaderMenuDesc;
objFrobDb.HeaderMenuActive = headerMenu.HeaderMenuActive;
objFrobDb.HeaderMenuDispOrder = headerMenu.HeaderMenuDispOrder;
_db.SaveChanges();
}
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
public DbSet<HeaderMenu> HeaderMenu { get; set; }
}
The problem is I cant to the Delete, Create or Edit operation!
I Belive that I can not make the connection between the Kendo and my Database and have to implement something in my controller.
I try the Kendo.MVC examples but the problem are the northwind and sqllite the used!
Could please help me by this issue to implement the CRUD on Grid using the data in my sqlserver tables.
I wanna achieve a simple task, which is to retrieve the binary image, and display it in my html
public class Artwork
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid artworkID { get; set; }
public string artworkName { get; set; }
public string artworkMimeType { get; set; }
public byte[] artworkMeta { get; set; }
public string artworkBase64String { get; set; }
}
Gets the artwork from DB
public Artwork GetArtwork(Guid id)
{
return _context.Artworks.SingleOrDefault(a => a.artworkID == id);
}
The API Controller
public IHttpActionResult Get(Guid id)
{
if (id == null)
{
return BadRequest();
}
var artwork = _repository.GetArtwork(id);
if (artwork == null)
return NotFound();
else
return Ok(artwork);
}
I've also used this method and it returns the data I want, but I still don't know how to use it to achieve my goal.
[HttpGet]
public HttpResponseMessage Get(Guid id)
{
HttpResponseMessage result = null;
try
{
var artwork = _repository.GetArtwork(id);
if (artwork == null)
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// sendo file to client
byte[] bytes = artwork.artworkMeta ;
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = artwork.artworkName;
}
return result;
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.Gone);
}
}
And here's my angular request
$scope.getCity = function (id) {
$http.get('/api/artwork/' + $RouteParams.id).success(function (response) {
$scope.artwork= response;
//I've seen dudes using Blob here, but I'm not sure how that works
});
}
My problem is my angular request and my html, how do I display the artwork without doing this:
<img ng-src="data:{{artwork.artworkartworkMimeType}};base64,{{artwork.artworkBase64String}}" class="img-responsive" />
This displays the image, but I don't like how clumsy it looks, and I'm gonna be working with audio files as well, so I need a clean and understandable way. Please help!
As you said, this can be done by using a blob.
First step is to set the content type to application/octet-stream in the api method
[HttpGet]
public HttpResponseMessage Get(Guid id)
{
HttpResponseMessage result = null;
try
{
var artwork = _repository.GetArtwork(id);
if (artwork == null)
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// sendo file to client
byte[] bytes = artwork.artworkMeta ;
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = artwork.artworkName;
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
}
return result;
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.Gone);
}
}
Then add the client request where you create a blob from the response. An url is then created for the blob which will be the source for the img
$scope.fileURL = '';
$http({
method: 'GET',
url: '/api/artwork/' + $RouteParams.id,
responseType: 'arraybuffer',
headers: {
'Access-Control-Allow-Origin': '*',
}
}).success(function (data, status, headers) {
headers = headers();
var contentType = headers['content-type'];
var blob = new Blob([data], { type: contentType });
//Create a url to the blob
$scope.fileURL = window.URL.createObjectURL(blob);
}).error(function (message) {
console.log(message);
});
Then bind url to the ngSrc
<img ng-src="{{fileURL}}" class="img-responsive" />
You could store image in binary format without encoding it to base64. Then it would be simpler to retrive image from DB.
In your asp controller:
[HttpGet]
public FileResult GetPhoto(int id) {
return File(_repository.GetArtwork(id).artworkMeta, "image/jpg");
}
And in angular view:
<img ng-src="/Home/GetPhoto/2" />
Similar questions have been asked so many times, but there are no clear answers, I still have trouble getting mine to work.
This is the model in C#
public class SubmitModel
{
public string Name { get; set; }
public HttpPostedFileBase File { get; set; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
This is the MVC code
[HttpPost]
public ActionResult Test(SubmitModel model)
{
// Here model.File and model.Files is always null
}
This is what I submitted using AngularJS
var data = {
name: scope.name, // This is passed to MVC successfully
file: scope.files[0], // Doesn't even work with single file
files: scope.files // This is a FileList
};
$http.post("/umbraco/surface/MyController/Test", data).success(...);
If you want to know how I assign scope.files:
$('#upload').on('change', function (e) {
scope.$apply(function () {
scope.files = e.target.files;
});
});
Could someone see what I am missing?
Solved it!
This is how it should be submitted
var data = new FormData();
angular.forEach(scope.item, function (value, key) {
if (key == "files") {
for (var i = 0; i < value.length; i++) {
data.append(value[i].name, value[i]); // Filename:File
}
} else {
data.append(key, value);
}
});
$http.post("/umbraco/surface/MyController/Test", data, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).success(...);
Then in MVC, we get the files from Request.Files, it won't be in the model.
[HttpPost]
public ActionResult Test(SubmitModel model)
{
var files = Request.Files; // a collection of HttpPostedFileBase
Save(model, files);
}
More info:
https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
http://www.codeproject.com/Tips/878730/File-Upload-Using-AngularJS-and-ASP-NET-MVC
I'm trying to send a IEnumerable from a web api controller to a AngularJs controller.
The code I was using was
Web Api:
readonly InventoryEntities _db = new InventoryEntities();
public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests.AsEnumerable();
}
AngularJS:
//get all customer information
$http.get("/api/Customer/").success(function (data) {
$scope.requests = data;
$scope.loading = false;
})
.error(function () {
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});
This worked fine, but now I'm using linq to include related tables and it doesn't work. The angularJs code is the same.
What am I doing wrong?
readonly InventoryEntities _db = new InventoryEntities();
public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
}
I do get the data I want in the controller, but when i try to send it back to angular, I get a 500 (Internal Server Error) angular.js:10722
This is what FDVOEligibilityRequest looks like in the new Web Api controller
public partial class FDVOEligibilityRequest
{
public int Id { get; set; }
public string TestName { get; set; }
public string GroupName { get; set; }
public int MandatoryFieldsID { get; set; }
public int DCCFieldsID { get; set; }
public virtual FDVOEligibilityRequestMandatoryField FDVOEligibilityRequestMandatoryField { get; set; }
public virtual FDVOEligibilityRequestDCCField FDVOEligibilityRequestDCCField { get; set; }
}
If it's 500 and happens after you successfully make a return from your action then it can be an exception during serialization.
I would suggest to check that there are no circular references between FDVOEligibilityRequest and FDVOEligibilityRequestMandatoryField/FDVOEligibilityRequestDCCField.
Or try to diagnose it using code from here:
string Serialize<T>(MediaTypeFormatter formatter, T value)
{
// Create a dummy HTTP Content.
Stream stream = new MemoryStream();
var content = new StreamContent(stream);
/// Serialize the object.
formatter.WriteToStreamAsync(typeof(T), value, stream, content, null).Wait();
// Read the serialized string.
stream.Position = 0;
return content.ReadAsStringAsync().Result;
}
try {
var val = _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
var str = Serialize(new JsonMediaTypeFormatter(), val);
}
catch (Exception x)
{ // break point here
}
ps: the common suggestion in this case is to use DTOs instead of EF objects with something like Automapper.
How do I structure the database? I use a repository pattern with entity framework and code-first to code the models.
For example: I want an admin to set a string to be appended to every username.
I was thinking about a key-value table (settings) that has the following columns? SettingsId, Name, Value. With this method, I would need to manually go in, create a record Name:AppendedToUsername, Value:nil. I would then write repository methods specifically for each settings I need. For eg.
public string GetAppenedToUsername()
{
db.Settings.FirstOrDefault(s => s.Name == "AppendedToUsername").Select(s => s.Value);
}
Is there any better way of designing this database?
It's a good solution. I only recommend to create a strongly typed class with these settings and use caching for them.
Cache service:
public class CacheService
{
private ObjectCache Cache
{
get { return MemoryCache.Default; }
}
public object Get(string key)
{
return Cache[key];
}
public void Set(string key, object data, int cacheTime)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now.AddMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
}
public bool IsSet(string key)
{
return (Cache[key] != null);
}
public void Invalidate(string key)
{
Cache.Remove(key);
}
}
AppSetting:
public class AppSetting
{
public const string StrSettingKey = "StrSetting";
private CacheService CacheService { get; set; }
private DbContext DbContext { get; set; }
public AppSetting(ICacheService cache, DbContext db)
{
CacheService = CacheService;
DbContext = db;
}
public string StrSetting
{
get
{
if (CacheService.IsSet(StrSettingKey))
{
return (string) CacheService.Get(StrSettingKey);
}
else
{
var value = DbContext.Settings.Single(s => s.Name == StrSettingKey).Select(s => s.Value);
CacheService.Set(StrSettingKey, value, 60); //one hour
return value;
}
}
set
{
var item = DbContext.Settings.Single(s => s.Name == StrSettingKey);
item.Value = value;
DbContext.SaveChanges();
CacheService.Set(StrSettingKey, value);
}
}
}