Asp.net MVC Ajax File Upload in Sitecore Programmatically using API calls

here I’m going to explain about uploading the file directly into Sitecore media library programmatically. below is the list of steps will be following to Upload the file to Sitecore

  • Coding cshtml & Ajax API calls
  • Coding to Create the Month & Year Wise Folders to store the uploading images
  • code to publish the created folders.

Code cshtml & Ajax API calls

Create a cshtml and add the following list of Code

<input type=”file” id=”FileUpload1″ />
<input type=”button” id=”btnUpload” value=”Upload Files” />

and then create a separate javascript file and add the following ajax API call functionality, it will help to pass the attached file to the form and it can also send the other input data

$(document).ready(function () {
$(‘#btnUpload’).click(function () {

// Checking whether FormData is available in browser
if (window.FormData !== undefined) {

var fileUpload = $(“#FileUpload1”).get(0);
var files = fileUpload.files;

// Create FormData object
var fileData = new FormData();

// Looping over all files and add it to FormData object
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}

// Adding one more key to FormData object
fileData.append(‘username’, ‘Manas’);

$.ajax({
url: ‘/Home/UploadImage’,
type: “POST”,
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: fileData,
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
} else {
alert(“FormData is not supported.”);
}
});
});

Create the Action Result and add the below code to Upload an Image,

1

Code to create Sitecore Folder programmatically

2.1

Code to attach the File to Sitecore Media Library

2

3

Code to Publish the newly Inserted media items 

4

Final View of the uploaded Images 

5

Advertisements

Bundling with Sitecore 6.5 to 8.2 + versions of Sitecore

Asp.net MVC4 bundling and minification it helps to avoid the repeated request to Servers and enables caching the data into server Memory.

I have got a chance to implement bundling in two Sitecore Versions

  • Sitecore 6.5 (Web Forms)
  • Sitecore 8.1 (MVC)

Let’s See how to implement bundling in Sitecore 6.5

Bundling in Sitecore 6.5 to  8.1 version

1)Open NuGet packet manager and execute the below command

2)Create a Class and add the .css file and .js file path as like below

3)Open Global.asax.cs file and open Application Start Event and add your bundling class

4)add the Script bundle and style bundle into the IgnoreUrlPrefixes as like below

5) Now remove the actual reference of your css &js files inside the code and add the below code to refer the files

Below is the reference in case of Web forms

 Bundling in Sitecore 8.2 and greater version

Sitecore removed the functionality of Global.asax.cs Application_Start event from Sitecore 8.2 onwards because its Inheriting Sitecore.Web.Application.  So we need to use the sitecore pipeline approach as per the blog post as below.

I already implemented asp.net bundling using Sitecore Pipeline as per the below blog post , its working absolutely fine

http://jockstothecore.com/bundling-with-sitecore-mvc/