Posts

Showing posts from August, 2015

Simple explanation and example about how to use FormData with a C# handler

well, you maybe read my prev post about how to send formData, maybe not, check here.

so now we want to build a  handler to get the file.

well the basic thing is that it comes as any other standard request, so

string[] formKeys = Context.Request.Form.AllKeys;
foreach (string key in formKeys) {
        var inputValue = Context.Request.Form[key];

so if its a file, you're type is IList<HttpPostedFile>, like this

IList<HttpPostedFile> file = Convert.ChangeType(
        Context.Request.Form[key], IList<HttpPostedFile>);

and when you have multiple files...

IList<HttpPostedFile> files =
    Context.Request.Files.GetMultiple(key);

and you must use "readAsDataURL" with the file reader in the client, so it encodes it as Base64, otherwise u'll have hard time to use it.

other than that is kinda simple.

FormData, send file with ajax, send ajax with a file, explanation, tutorial, and example, in c# asp.net

so... this will be the table of content for everything i wrote in this long, long title
FormData simple overview the most simple way to define it is, as simple as that, do a full postback without a postback, just with ajax.
usually you'll need it in 2 scenarios - (a) uploading file/s, maybe with other properties. (b) security won't let you do JSON.

so what i made for you?

1. Detailed simple explanation about the examples in developer.mozilla.org docs, its a bit hard to get examples.
2. Detailed simple explanation and example about how to make it more JQuery and generic
3. Simple explanation and example about how to use it with a C# handler

HF!!

Detailed simple explanation and example about how to make it more JQuery and generic

well, if you read my post about how to send a FormData here you probably said something like "why can it be more simple, just to send a bloody ajax with a file", well ur in the right place.
also TOC about this subject here like server side

this is the basically the same code only a bit refined to FormData only, and skipping the part where its constrained to a form

also added the __REQUESTDIGEST for anyone who wants to use this with sharepoint, just remove the comment there.

NOTE that i changed the readAsBinaryString to readAsDataURL since it solves some encoding problems

now the function accepts a jQuery object as a "form" and just runs on all of its elements, and you get to send yourself the normal ajas options object with the URL and all other headers ect. and callbacks (success, error..)

var FormDataAJAXSubmit = (function () {
   function submitData(oData) {
   var sBoundary = "---------------------------" + Date.now().toString(16);
oData.ajaxOptions.conte…

Detailed simple explanation about the examples in developer.mozilla.org docs, its a bit hard to get examples

have you read this https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files?

or the more basic one https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects?

a bit hard and un-copy-pasteable. after that you may read my own a bit more generic exmamle.

so lets start with the 2nd link, which is supposely simple, but there are a couple of catches.

1st, although it says, you can only create a FormData with a form element, which is a bit problematic in many cases where you can only have 1 form like in asp.net or sharepoint.

the solution will be presented down here with the explanation about the upper link, and in a more generic example in my other post here

2nd is in the jquery example, where they say you can turn off the proccessData and contentType, which although true, but that means you need to break the request hardcoded, and that in managed frameworks you'll get the raw stream of the request in…

Sharepoint 2013 search filer and refine taxonomy managed metadata field equal exact match

if u'll use the "Search Query Tools v2", u'll find out an interesting thing, the results of an MMD field contains a strain of guids, and a single strain for word,

the guids are the the guid of ur selected MMD and his childs.
the string is the label.

so what to so if you have a tree with lv1 say "Batman" and lv2 "Batman and Robin", and you want to retrieve only "Batman"?

if you put the "Batman" guid u'll get many resutls, if you put "Batman" u'll get many results.

looking in that awful string i noticed something, the really selected MMD has its guid twice, once usual, and the other with "0" (zero) before.

so just put in your query "0" (zero) before the guid.