Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4uploadify

How to set an error as return on controller and get it on uploadify in asp net mvc 4?


I am out of Ideas. The way the system is built I am not sure I can do what I have to do.

This is the controller that processes the uploaded file coming from uploadify.

public ActionResult Upload(HttpPostedFileBase FileData, FormCollection form)
{
     try 
     {
        String path = String.Format("{0}{1}", caminhoArmazenamento, tipoDocNome);

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
            FileData.SaveAs(path);
            //HERE I NEED SOME CONTROL TO RETURN THAT THINGS WENT RIGHT
        }
        else
        {
            FileData.SaveAs(path);
            //HERE I NEED SOME CONTROL TO RETURN THAT THINGS WENT RIGHT
        }
    }
    catch(exception)
    {
       //HERE I NEED SOME CONTROL SAYING THAT THINGS WENT WRONG
    }
}

This is the uploadify:

$('#file_upload').uploadify({
        'swf': '../../Components/uploadify/uploadify.swf',
        'uploader': '/Operacao/Upload',
        'auto': false,
        'buttonImage': '../../Images/uploadify/importar.jpg',
        'buttonClass': 'uploadifyBtn',
        'width': '250',
        'height': '25',
        'onFallback': function () {
            alert('Versão do flash não compativel com o sistema de upload. Favor contactar o administrador do sistema!');
        },
        'onUploadError' : function(file, errorCode, errorMsg, errorString) {
            alert('O arquivo ' + file.name + ' não pode ser importado: ' + errorCode + ' - ' + errorMsg + ' - ' + errorString);
        },
        'onSelectError': function () {
            alert('Você não tem permissão para acessar o arquivo: "' + file.name + '" ou o arquivo está corrompido. Favor contactar o administrador do sistema.');
        }
    });

Does anybody have sucessfully implement uploadfy in asp net mvc 4? Does not matter what I set as return, I only get a or a general error or an success mensagem. I need to implement error control more specific, for example if a directory could not be created or the file could not be uploaded and why. Thanks


Solution

  • From what I can tell, depending on the return server code, uploadify will execute different callbacks.

    Here is an example of the server returning a 404 and how uploadify response: http://www.uploadify.com/documentation/uploadify/onuploaderror/

    What you'll want to do is return the appropriate Response.StatusCode (200, 404, 501) depending on what happens.

    How to get MVC action to return 404