1

I want to upload a image with a description like:

data = '{"filename":"' + myfilename + '", "file":"' + file + '", "description":"' +
    description + '"}';

$.ajax({
    type: "POST",
    url: "filehandler.ashx",
    data: data,
    success: function (result) {
        alert(result);
    },
    error: function () {
        alert("There was error uploading file!");
    }
});

how can I do it? I can't read file as HttpPostedFile in generic handler. context.Request.Form also doesn't have any keys.

3
  • Please add the problematic code Commented Aug 15, 2012 at 7:31
  • @AmiramKorach I don't know how to write handler to get file and description separately. Commented Aug 15, 2012 at 7:37
  • With this code, I do not see how you could upload anything. Here is a post of a js object containing strings. Uploading a file cannot be achieved this way unfortunately. Basically it's the browser's job to set the file name (for security reasons). now maybe I did not got your snippet. Commented Aug 15, 2012 at 8:18

2 Answers 2

5

I'm sorry, If I not posted fully what I did in question. Anyway I got it to work.

var data = new FormData();

data.append("name", filename);
data.append("file", file);

In generic handler

HttpPostedFile file = context.Request.Files["file"];
string fileName = context.Request.Form["filename"];

Using FormData Objects

Sign up to request clarification or add additional context in comments.

1 Comment

Only work in recent browsers tho, but since IE10 now also works on Win7 it's usable :)
2

you cant post files with ajax you have to post it in a iframe to have the same result

this is what I did

var form = "#myform";
var url = "http://post.it/";
var iframeName = 'iframePost' + (new Date()).getTime(); 
$('<iframe id="'+iframeName+'" name="' + iframeName + '" style="display:none;"/>').appendTo('body');
$(form).attr('target',iframeName)
    .attr('action',url)
    .attr('enctype','multipart/form-data')
    .append('<input type="hidden" name="_iframe" value="' + iframeName + '" />');
    form.submit();

to have a callback let the iframe return a script with something like

window.parent.callBack(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.