Ecco il mio codice:
$("#yourID").kendoEditor(
{
tools:
[
...
],
messages: {
...
},
encoded: false,
imageBrowser: {
messages: {
dropFilesHere: "Drop and Drag Images"
},
transport: {
read: {
url: "ImageLogic/ReadImage",
dataType: "json",
type: "POST"
},
destroy: {
url: "ImageLogic/DestroyImage",
type: "POST"
},
create: {
url: "ImageLogic/CreateImage",
type: "POST"
},
thumbnailUrl: "ImageLogic/Thumbnail",
uploadUrl: "ImageLogic/UploadImage",
imageUrl: baseUrl + "Upload/Images/{0}" //baseUrl is your root url, for ex: http://yourwebsitename/Upload/Images/test.png
}
}
})
E nel mio Controller:
private const string DefaultFilter = "*.png,*.gif,*.jpg,*.jpeg";
private const int ThumbnailHeight = 80;
private const int ThumbnailWidth = 80;
private const string ImagePath = "Upload/Editor";
private readonly DirectoryBrowser directoryBrowser;
private readonly ThumbnailCreator thumbnailCreator;
public ImageLogicController()
{
directoryBrowser = new DirectoryBrowser();
thumbnailCreator = new ThumbnailCreator();
}
[HttpPost]
public ActionResult ReadImage(string path)
{
try
{
string _path = string.IsNullOrEmpty(path) ? ("~/" + ImagePath) : ("~/" + ImagePath + "/" + path);
directoryBrowser.Server = Server;
var result = directoryBrowser
.GetContent(_path, DefaultFilter)
.Select(f => new
{
name = f.Name,
type = f.Type == EntryType.File ? "f" : "d",
size = f.Size
});
return Json(result, JsonRequestBehavior.AllowGet);
}
catch (DirectoryNotFoundException)
{
throw new HttpException(404, "File Not Found");
}
}
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult DestroyImage(string path, string name, string type)
{
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type))
{
path = Path.Combine(Server.MapPath("~/" + ImagePath), name);
if (type.ToLowerInvariant() == "f")
{
DeleteFile(path);
}
else
{
DeleteDirectory(path);
}
return Json(null);
}
throw new HttpException(404, "File Not Found");
}
protected virtual void DeleteFile(string path)
{
var physicalPath = Path.Combine(Server.MapPath("~/" + ImagePath), path);
if (System.IO.File.Exists(physicalPath))
{
System.IO.File.Delete(physicalPath);
}
}
protected virtual void DeleteDirectory(string path)
{
var physicalPath = Path.Combine(Server.MapPath("~/" + ImagePath), path);
if (Directory.Exists(physicalPath))
{
Directory.Delete(physicalPath, true);
}
}
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult CreateImage(string path, FileBrowserEntry entry)
{
var name = entry.Name;
if (!string.IsNullOrEmpty(name))
{
var physicalPath = Path.Combine(Server.MapPath("~/" + ImagePath), name);
if (!Directory.Exists(physicalPath))
{
Directory.CreateDirectory(physicalPath);
}
return Json(null);
}
throw new HttpException(403, "Forbidden");
}
[OutputCache(Duration = 3600, VaryByParam = "path")]
public virtual ActionResult Thumbnail(string path)
{
var imgPath = Path.Combine(Server.MapPath("~/" + ImagePath), path);
if (System.IO.File.Exists(imgPath))
{
Response.AddFileDependency(imgPath);
return CreateThumbnail(imgPath);
}
throw new HttpException(404, "File Not Found");
}
private FileContentResult CreateThumbnail(string physicalPath)
{
using (var fileStream = System.IO.File.OpenRead(physicalPath))
{
var desiredSize = new ImageSize
{
Width = ThumbnailWidth,
Height = ThumbnailHeight
};
const string contentType = "image/png";
return File(thumbnailCreator.Create(fileStream, desiredSize, contentType), contentType);
}
}
[HttpPost]
public ActionResult UploadImage(string path, HttpPostedFileBase file)
{
var fileName = Path.GetFileName(file.FileName);
var oFileName = Path.GetFileNameWithoutExtension(file.FileName);
var extension = Path.GetExtension(file.FileName);
string temp = DateTime.UtcNow.Ticks.ToString();
string newFileName = oFileName + "_" + temp + extension;
string _path = string.IsNullOrEmpty(path) ? ("~/" + ImagePath) : ("~/" + ImagePath + "/" + path);
var physPath = Path.Combine(Server.MapPath(_path), file.FileName);
file.SaveAs(physPath);
return Json(new { name = file.FileName, type = "f", size = file.ContentLength });
}
[OutputCache(Duration = 360, VaryByParam = "path")]
public ActionResult Image(string path)
{
var physicalPath = Path.Combine(Server.MapPath("~/" + ImagePath), path);
if (System.IO.File.Exists(physicalPath))
{
const string contentType = "image/png";
return File(System.IO.File.OpenRead(physicalPath), contentType);
}
throw new HttpException(403, "Forbidden");
}
Spero che ti aiuta. Grazie.
Si prega di fornire la lingua in cui si sta integrando il codice poiché kendo-ui supporta varie lingue. Sia puramente php o html5 e javascript con la lingua lato server, così posso aiutare di conseguenza. –