I have a radEditor control with the ImageManager enabled. this feature worked fine within our last version we had (2011 version) but now with the version we have, the image manager does not insert the image selected. Below is my radEditor html tag:
<telerik:RadEditor ID="txtRTE"
SpellCheckSettings-AllowAddCustom="false"
ToolsFile="~/SimpleRTEEditorTools.xml"
OnClientLoad="HandleRTEClientLoad"
ExternalDialogsPath="~/RadControls/EditorDialogs/"
runat="server"
Height="200"
Skin="Default"
EditModes="Design"
AllowScripts="false"
StripFormattingOptions="All">
<ImageManager ViewPaths=".." UploadPaths=".." SearchPatterns="*.jpg,*.gif,*.png" EnableImageEditor="False" ViewMode="Grid" />
I am editing the ViewPaths and uploadPaths within the code behind's page_load method:
txtRTE.ImageManager.ViewPaths = paths;
txtRTE.ImageManager.UploadPaths = paths;
txtRTE.ImageManager.ContentProviderTypeName = typeof(FolderContentProvider).AssemblyQualifiedName;
We implemented our own content provider as seen below:
public class FolderContentProvider : FileBrowserContentProvider
{
private string ROOT_DIRECTORY_FULL_PATH =
//Path to record documents folder
System.Configuration.ConfigurationManager.AppSettings[Constants.RECORD_DOC_ROOT_FOLDER_APP_KEY].ToString() +
//folder containing images
Constants.Record_DOC_FORM_TEXT_IMAGE_FOLDER + "\\" +
//to get Record ID
((MyAppPrincipal)System.Threading.Thread.CurrentPrincipal).Record.ID;
public string RootDirectory
{
get
{
return ROOT_DIRECTORY_FULL_PATH;
}
private set
{
}
}
private PathPermissions fullPermissions = PathPermissions.Read | PathPermissions.Upload;
private DirectoryItem[] GetSubDirectories(string path)
{
//we have only one directory no sub directories
//no need to go to file system to find that out
return new DirectoryItem[0];
}
private string GetDirectoryFullPath(string path)
{
return RootDirectory;
}
private FileItem[] GetFiles(string path)
{
string[] filesFullName = Directory.GetFiles(RootDirectory);
ArrayList files = new ArrayList();
for (int i = 0; i < filesFullName.Length; i++)
{
string fullPath = filesFullName[i];
System.IO.FileInfo currentFile = new System.IO.FileInfo(fullPath);
if (IsAlowedFileExtension(currentFile.Extension))
{
string url = string.Format("{0}?path={1}", HttpContext.Current.Request.ApplicationPath + "/app/FormTextImageHandler.ashx", currentFile.Name);
files.Add(new FileItem(
currentFile.Name, //file name
currentFile.Extension, //extension
currentFile.Length, //size
string.Empty,//currentFile.FullName, //location
url, //url
string.Empty,//tag
fullPermissions//permissions
));
}
}
return (FileItem[])files.ToArray(typeof(FileItem));
}
private bool IsAlowedFileExtension(string Extension)
{
if (Extension.Equals(".gif", StringComparison.InvariantCultureIgnoreCase))
return true;
if (Extension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))
return true;
if (Extension.Equals(".png", StringComparison.InvariantCultureIgnoreCase))
return true;
return false;
}
public FolderContentProvider(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag)
: base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
{
}
public override string DeleteFile(string path)
{
//we do not allow removing files
return null;
}
public override string DeleteDirectory(string path)
{
//we don't have any sub directories
//and moreover we don't give delete rights
return null;
}
public override string StoreFile(Telerik.Web.UI.UploadedFile file, string path, string name, params string[] arguments)
{
int fileLength = (int)file.InputStream.Length;
byte[] content = new byte[fileLength];
file.InputStream.Read(content, 0, fileLength);
string fullPath = RootDirectory +"\\"+ name;
FileStream fileStream = new FileStream(fullPath, FileMode.OpenOrCreate);
fileStream.Write(content, 0, content.Length);
fileStream.Flush();
fileStream.Close();
return string.Empty;
}
public override DirectoryItem ResolveDirectory(string path)
{
DirectoryItem[] directories = new DirectoryItem[0];
FileItem[] files = this.GetFiles(RootDirectory);
DirectoryItem dir = new DirectoryItem("Images", string.Empty, RootDirectory, string.Empty, fullPermissions, files, directories);
return dir;
}
public override DirectoryItem ResolveRootDirectoryAsTree(string path)
{
//we don't have any subdirectories - everythinng is in the same folder
DirectoryItem[] directories = new DirectoryItem[0];
FileItem[] files = this.GetFiles(RootDirectory);
DirectoryItem root = new DirectoryItem("Images", string.Empty, "Images\\", string.Empty, fullPermissions, files, directories);
return root;
}
public override bool CanCreateDirectory
{
get
{
return false;
}
}
public override string CreateDirectory(string path, string name)
{
return null;
}
public override string StoreBitmap(Bitmap bitmap, string url, ImageFormat format)
{
return null;
}
public override Stream GetFile(string url)
{
return null;
}
public override string GetPath(string url)
{
return RootDirectory;
}
public override string GetFileName(string url)
{
return null;
}
[Obsolete]
public override DirectoryItem[] ResolveRootDirectoryAsList(string path)
{
return null;
}
public override bool CheckWritePermissions(string folderPath) {
return true;
}
}
Any idea's why the old version was able to insert into the field, but the new version is not?
The FileBrowserProvider API could be changed between the old and new versions. That why my suggestion is to examine the code of the following demo http://demos.telerik.com/aspnet-ajax/editor/examples/dbfilebrowsercontentprovider/defaultcs.aspx that works as expected and compare it with the code of your custom solution.
If you have any customized dialogs you may need to copy the EditorDialogs folder from the new installation that you are using and customize them from scratch.
Best regards,
Rumen
Related
I tried this solution: Custom fields with FormBuilder in the Microsoft Bot Framework
But failed to get it working....The problem I encountered is that when I assign the base.Form = value, the _prompt in the _field gets a default recognizer, and it won't get overriden in the next line's SetRecognizer call, that only replaces the _field's recognizer.
However the matching process uses the _prompt's recognizer internally ( ? ).
Here is my code:
public class LuisIntentRecognizer<T> : RecognizePrimitive<T>
where T : class
{
public LuisIntentRecognizer(IField<T> field, string luisModelID, string luisSubscriptionKey)
: base(field)
{
_luisModelID = luisModelID;
_luisSubscriptionKey = luisSubscriptionKey;
}
public override DescribeAttribute ValueDescription(object value)
{
return new DescribeAttribute((string)value);
}
public override IEnumerable<string> ValidInputs(object value)
{
yield return (string)value;
}
public override TermMatch Parse(string input)
{
TermMatch result = null;
if (!string.IsNullOrWhiteSpace(input))
{
var luisModel = new LuisModelAttribute(_luisModelID, _luisSubscriptionKey);
var luisService = new LuisService(luisModel);
var luisResult = luisService.QueryAsync(input).Result; // TODO refactor somehow to async
var winner = luisResult.Intents.MaxBy(i => i.Score ?? 0d);
if (winner != null && !string.IsNullOrEmpty(winner.Intent))
{
result = new TermMatch(0, winner.Intent.Length, 0.0, winner.Intent);
}
else
{
result = new TermMatch(0, input.Length, 0.0, input);
}
}
return result;
}
public override string Help(T state, object defaultValue)
{
var prompt = new Prompter<T>(_field.Template(TemplateUsage.StringHelp), _field.Form, null);
var args = HelpArgs(state, defaultValue);
return prompt.Prompt(state, _field.Name, args.ToArray()).Prompt;
}
private string _luisModelID;
private string _luisSubscriptionKey;
}
public class LuisIntentField<T> : FieldReflector<T>
where T : class
{
public LuisIntentField(string name, string luisModelID, string luisSubscriptionKey, bool ignoreAnnotations = false)
: base(name, ignoreAnnotations)
{
_luisModelID = luisModelID;
_luisSubscriptionKey = luisSubscriptionKey;
}
public override IForm<T> Form
{
set
{
base.Form = value;
base.SetRecognizer(new LuisIntentRecognizer<T>(this, _luisModelID, _luisSubscriptionKey));
}
}
private string _luisModelID;
private string _luisSubscriptionKey;
}
Could anyone get it working?
Thanks
It seems to be a bug in the framework indeed: https://github.com/Microsoft/BotBuilder/issues/879
I have a SqlCe database which i made for another project. Now i want to use it for a windows phone project. My database structure is
I copied my database into my project folder and set it's build action as "content" and copy to output directory as "copy always".
In my main page i used this:
private const string Con_String = #"isostore:/mydb.sdf";
public MainPage()
{
InitializeComponent();
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
using (mTableDatabaseContext context = new mTableDatabaseContext(Con_String))
{
if (!context.DatabaseExists())
{
context.CreateDatabase();
}
if (!iso.FileExists("mydb.sdf"))
{
MoveReferenceDatabase();
}
}
}
public static void MoveReferenceDatabase()
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
using (Stream input = Application.GetResourceStream(new Uri("mydb.sdf", UriKind.Relative)).Stream)
{
using (IsolatedStorageFileStream output = iso.CreateFile("mydb.sdf"))
{
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
output.Write(readBuffer, 0, bytesRead);
}
}
}
}
and my mTableDatabaseContext class is like that:
public class mTableDatabaseContext:DataContext
{
public mTableDatabaseContext(string connectionString): base(connectionString)
{
}
public Table<dic> my_dics
{
get
{
return this.GetTable<dic>();
}
}
public Table<learn_table> my_learn_tables
{
get
{
return this.GetTable<learn_table>();
}
}
}
But i cant use my database and copy of my database cant be performed???
What can i do to do this??
How can i do this?? Can anyone help me??
You should use
private const string Con_String = #"Data Source=isostore:/mydb.sdf";
instead of
private const string Con_String = #"isostore:/mydb.sdf";
What I want to do is simply to upload a photo to a webservice using mono touch/mono droid and mvvmcross, hopefully in a way so I only have to write the code once for both android and IOS :)
My initial idea is to let the user pick an image (in android using an intent) get the path for the image. Then use MvxResourceLoader resourceLoader to open an stream from the path and then use restsharp for creating a post request with the stream.
However I already hit a wall, when the user picks an image the path is e.g. "/external/images/media/13". this path results in a file not found exception when using the MvxResourceLoader resourceLoader.
Any ideas to why I get the exception or is there an better way to achieve my goal?
This is how I ended up doinging it - thank you stuart and to all the links :)
public class PhotoService :IPhotoService, IMvxServiceConsumer<IMvxPictureChooserTask>,IMvxServiceConsumer<IAppSettings>
{
private const int MaxPixelDimension = 300;
private const int DefaultJpegQuality = 64;
public void ChoosePhotoForEventItem(string EventGalleryId, string ItemId)
{
this.GetService<IMvxPictureChooserTask>().ChoosePictureFromLibrary(
MaxPixelDimension,
DefaultJpegQuality,
delegate(Stream stream) { UploadImage(stream,EventGalleryId,ItemId); },
() => { /* cancel is ignored */ });
}
private void UploadImage(Stream stream, string EventGalleryId, string ItemId)
{
var settings = this.GetService<IAppSettings>();
string url = string.Format("{0}/EventGallery/image/{1}/{2}", settings.ServiceUrl, EventGalleryId, ItemId);
var uploadImageController = new UploadImageController(url);
uploadImageController.OnPhotoAvailableFromWebservice +=PhotoAvailableFromWebservice;
uploadImageController.UploadImage(stream,ItemId);
}
}
public class PhotoStreamEventArgs : EventArgs
{
public Stream PictureStream { get; set; }
public Action<string> OnSucessGettingPhotoFileName { get; set; }
public string URL { get; set; }
}
public class UploadImageController : BaseController, IMvxServiceConsumer<IMvxResourceLoader>, IMvxServiceConsumer<IErrorReporter>, IMvxServiceConsumer<IMvxSimpleFileStoreService>
{
public UploadImageController(string uri)
: base(uri)
{
}
public event EventHandler<PhotoStreamEventArgs> OnPhotoAvailableFromWebservice;
public void UploadImage(Stream stream, string name)
{
UploadImageStream(stream, name);
}
private void UploadImageStream(Stream obj, string name)
{
var request = new RestRequest(base.Uri, Method.POST);
request.AddFile("photo", ReadToEnd(obj), name + ".jpg", "image/pjpeg");
//calling server with restClient
var restClient = new RestClient();
try
{
this.ReportError("Billedet overføres", ErrorEventType.Warning);
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//upload successfull
this.ReportError("Billedet blev overført", ErrorEventType.Warning);
if (OnPhotoAvailableFromWebservice != null)
{
this.OnPhotoAvailableFromWebservice(this, new PhotoStreamEventArgs() { URL = base.Uri });
}
}
else
{
//error ocured during upload
this.ReportError("Billedet kunne ikke overføres \n" + response.StatusDescription, ErrorEventType.Warning);
}
});
}
catch (Exception e)
{
this.ReportError("Upload completed succesfully...", ErrorEventType.Warning);
if (OnPhotoAvailableFromWebservice != null)
{
this.OnPhotoAvailableFromWebservice(this, new PhotoStreamEventArgs() { URL = url });
}
}
}
//method for converting stream to byte[]
public byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position;
stream.Position = 0;
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
stream.Position = originalPosition;
}
}
}
Try:
Issues taking images and showing them with MvvmCross on WP
Need an example of take a Picture with MonoDroid and MVVMCross
https://github.com/Redth/WshLst/ - uses Xam.Mobile for it's picture taking
I have a fileupload function where users can upload files. I want to restrict the users from upload certain file types. The types allowed are: .doc,.xlsx,.txt,.jpeg.
How I can do this?
This is my actual file upload code:
public ActionResult UploadFile(string AttachmentName, BugModel model)
{
BugModel bug = null;
if (Session["CaptureData"] == null)
{
bug = model;
}
else
{
bug = (BugModel)Session["CaptureData"];
}
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file1 = Request.Files[inputTagName];
if (file1.ContentLength > 0)
{
string path = "/Content/UploadedFiles/" + Path.GetFileName(file1.FileName);
string savedFileName = Path.Combine(Server.MapPath("~" + path));
file1.SaveAs(savedFileName);
BugAttachment attachment = new BugAttachment();
attachment.FileName = "~" + path.ToString();
attachment.AttachmentName = AttachmentName;
attachment.AttachmentUrl = attachment.FileName;
bug.ListFile.Add(attachment);
model = bug;
Session["CaptureData"] = model;
}
}
ModelState.Clear();
return View("LoadBug", bug);
}
The first thing to verify is whether the file extension contained in file1.FileName matches one of the allowed extensions. Then if you really want to ensure that the user hasn't renamed some other file type to an allowed extension you will need to look into the contents of the file to recognize whether it is one of the allowed types.
Here's an example how to check whether the file extension belongs to a list of predefined extensions:
var allowedExtensions = new[] { ".doc", ".xlsx", ".txt", ".jpeg" };
var extension = Path.GetExtension(file1.FileName);
if (!allowedExtensions.Contains(extension))
{
// Not allowed
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class AllowedFileExtensionAttribute : ValidationAttribute
{
public string[] AllowedFileExtensions { get; private set; }
public AllowedFileExtensionAttribute(params string[] allowedFileExtensions)
{
AllowedFileExtensions = allowedFileExtensions;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var file = value as HttpPostedFileBase;
if (file != null)
{
if (!AllowedFileExtensions.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase)))
{
return new ValidationResult(string.Format("{1} için izin verilen dosya uzantıları : {0} : {2}", string.Join(", ", AllowedFileExtensions), validationContext.DisplayName, this.ErrorMessage));
}
}
return null;
}
}
Usage In Model
[AllowedFileExtension(".jpg", ".png", ".gif", ".jpeg")]
public HttpPostedFileBase KategoriResmi { get; set; }
You can use the ContentType property of the HttpPostedFileBase for a basic check of the file type (mime type): See MSDN's page on the Content-Type property here
Here is one way to do it:
private static bool IsValidContentType(string contentType)
{
string ct = contentType.ToLower();
return ((ct == "application/msword") || (ct == "application/pdf") || (ct == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
}
etc..
However, for a deeper inspection, you will have to inspect the file content. It's easy to change a file extension..
I want to replicate what StackOverflow does with its URLs.
For example:
Hidden Features of C#? - (Hidden Features of C#?)
or
Hidden Features of C#? - (Hidden Features of C#?)
Will Take you to the same page but when they return to the browser the first one is always returned.
How do you implement the change so the larger URL is returned?
The way that I've handled this before is to have two routes, registered in this order
routes.MapRoute(
null,
"questions/{id}/{title}",
new { controller = "Questions", action = "Index" },
new { id = #"\d+", title = #"[\w\-]*" });
routes.MapRoute(
null,
"questions/{id}",
new { controller = "Questions", action = "Index" },
new { id = #"\d+" });
now in the controller action,
public class QuestionsController
{
private readonly IQuestionRepository _questionRepo;
public QuestionsController(IQuestionRepository questionRepo)
{
_questionRepo = questionRepo;
}
public ActionResult Index(int id, string title)
{
var question = _questionRepo.Get(id);
if (string.IsNullOrWhiteSpace(title) || title != question.Title.ToSlug())
{
return RedirectToAction("Index", new { id, title = question.Title.ToSlug() }).AsMovedPermanently();
}
return View(question);
}
}
We'll permanently redirect to the URL that contains the title slug (lowercase title with hyphens as separators) if we only have the id. We also make sure that the title passed is the correct one by checking it against the slugged version of the question title, thereby creating a canonical URL for the question that contains both the id and the correct title slug.
A couple of the helpers used
public static class PermanentRedirectionExtensions
{
public static PermanentRedirectToRouteResult AsMovedPermanently
(this RedirectToRouteResult redirection)
{
return new PermanentRedirectToRouteResult(redirection);
}
}
public class PermanentRedirectToRouteResult : ActionResult
{
public RedirectToRouteResult Redirection { get; private set; }
public PermanentRedirectToRouteResult(RedirectToRouteResult redirection)
{
this.Redirection = redirection;
}
public override void ExecuteResult(ControllerContext context)
{
// After setting up a normal redirection, switch it to a 301
Redirection.ExecuteResult(context);
context.HttpContext.Response.StatusCode = 301;
context.HttpContext.Response.Status = "301 Moved Permanently";
}
}
public static class StringExtensions
{
private static readonly Encoding Encoding = Encoding.GetEncoding("Cyrillic");
public static string RemoveAccent(this string value)
{
byte[] bytes = Encoding.GetBytes(value);
return Encoding.ASCII.GetString(bytes);
}
public static string ToSlug(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
var str = value.RemoveAccent().ToLowerInvariant();
str = Regex.Replace(str, #"[^a-z0-9\s-]", "");
str = Regex.Replace(str, #"\s+", " ").Trim();
str = str.Substring(0, str.Length <= 200 ? str.Length : 200).Trim();
str = Regex.Replace(str, #"\s", "-");
str = Regex.Replace(str, #"-+", "-");
return str;
}
}