I am building an Umbraco7 Package and I'm at a bit of a standstill. I have very little experience with Angular and C# and what I'm trying to do is add an import option where a backoffice user can import an xls file and populate an existing database table with the xls contents.
This is what I have so far, I'm not sure how to get it to save the file in the firs place, let alone extract the values from the xls file.
HTML
// This is the view (table.html)
// Excluding the export blank xls file table.
<div class="import-div">
<input type="file" name="MyFile" />
<input type="submit" class="btn btn-danger" ng-click="ButtonClickHandler()" />
</div>
Angular.Js Controller file
// Here is my js controller file:
angular.module("umbraco")
.controller("ImportCtrl", function ($scope, keyResource) {
$scope.ButtonClickHandler = function () {
console.log("I was clicked!");
};
});
C# Controller
//This is the C# controller.
using AngularUmbracoPackage.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using umbraco.cms.businesslogic.member;
using Umbraco.Web.Mvc;
using Umbraco.Core.Persistence;
using System.Configuration;
using Umbraco.Core.Logging;
using System.Data;
using System.Text;
using System.IO;
using System.Web;
using System.Net.Http;
namespace AngularUmbracoPackage.App_Code
{
[HttpPost]
public class ImportNewDictionaryController
{
[HttpPost]
public ActionResult importFile()
{
public string fileName;
public string DictionaryKey;
var db = UmbracoContext.Application.DatabaseContext.Database;
var insert = new Sql("INSERT INTO cmsDictionary VALUES('" + DictionaryKey + "'");
}
}
}
Can anyone give me any links which may help or guide me through this?
This is how I managed to get it working!
using UmbracoImportExportPlugin.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Persistence;
using Umbraco.Web;
using Umbraco.Web.WebApi;
namespace UmbracoImportExportPlugin.App_Code
{
public class ImportNewDictionaryController : UmbracoAuthorizedApiController
{
public string basePath;
//Locate specific path
public void LocatePath()
{
this.basePath = System.Web.Hosting.HostingEnvironment.MapPath(@"/upload");
}
[System.Web.Http.AcceptVerbs("GET", "POST")]
//[System.Web.Http.HttpPost]
public void SaveFile()
{
var myContext = Request.TryGetHttpContext();
List<string> keys = new List<string>();
if (myContext.Success)
{
HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
if (myFile == null)
{
throw new HttpException("invalid file");
}
else
{
StreamReader csvreader = new StreamReader(myFile.InputStream);
while (!csvreader.EndOfStream)
{
var line = csvreader.ReadLine();
if (line != "Key")
keys.Add(line);
}
}
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var remove = new Sql("DELETE FROM cmsDictionary");
int rem = db.Execute(remove);
foreach (string item in keys)
{
var insert = new Sql("INSERT INTO cmsDictionary VALUES (NEWID(), null,'" + item + "')");
int res = db.Execute(insert);
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With