Created
January 23, 2013 20:42
-
-
Save rauhryan/4612880 to your computer and use it in GitHub Desktop.
Ajax file upload with jquery.forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AjaxFileUploadWriter<T> : IMediaWriter<T> | |
{ | |
readonly IOutputWriter _writer; | |
public AjaxFileUploadWriter(IOutputWriter writer) | |
{ | |
_writer = writer; | |
} | |
public void Write(string mimeType, T resource) | |
{ | |
var rawJsonOutput = JsonUtil.ToJson(resource); | |
// For proper jquery.form plugin support of file uploads | |
// See the discussion on the File Uploads sample at http://malsup.com/jquery/form/#code-samples | |
var html = "<html><body><textarea rows=\"10\" cols=\"80\">" + rawJsonOutput + | |
"</textarea></body></html>"; | |
_writer.Write(MimeType.Html.ToString(), html); | |
} | |
public IEnumerable<string> Mimetypes | |
{ | |
get { yield return MimeType.Html.Value; } | |
} | |
} | |
public class AjaxFileUploadPolicy : IConfigurationAction | |
{ | |
public void Configure(BehaviorGraph graph) | |
{ | |
//note: OutputNode<T> -> OutputNode - I may have gaffed something. | |
graph.Behaviors.Where(Filter) | |
.Each(c => | |
{ | |
c.AlterConnegOutput(output => | |
{ | |
output.AddWriter<AjaxFileUploadWriter<AjaxContinuation>>(); | |
}); | |
}); | |
} | |
public static bool Filter(BehaviorChain chain) | |
{ | |
return chain.ResourceType().CanBeCastTo<AjaxContinuation>() | |
&& chain.CheckFirstCall(c => c.HasAttribute<WrapJsonOutputInTextarea>()); | |
} | |
} | |
[AttributeUsage(AttributeTargets.Method)] | |
public class WrapJsonOutputInTextarea : Attribute | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment