Upload multiple files using Uploadfy in ASP.NET MVC

Asked

Viewed 1,354 times

2

How to implement multi-file upload with uploadf? I find it very interesting to use the upload bars etc.

is making this mistake:

This is my controller:

You may notice that you have many error messages as well.

View code:

<script type="text/javascript">
    $(function () {
        $('#file_upload').uploadify({
            'swf': "@Url.Content("~/Content/UploadifyContent/uploadify.swf")",
            'cancelImg': "@Url.Content("~/Content/UploadifyContent/uploadify-cancel.png")",
            'uploader': "@Url.Action("Upload", "Photo")",            
            'buttonText': 'SELECIONAR',
            'onUploadSuccess': function (file, data, response) {
                $("#uploaded").append("<img src='" + data + "' alt='Uploaded Image' />");
            }
        });
    });
</script>

1 answer

4


The original answer recommended using the Nuget package:

https://www.nuget.org/packages/Uploadfy/

But it is old and may not fit if the jQuery version is the latest. Then download the Zip at this address:

http://www.uploadify.com/download/

Put the files into your project as follows:

  • ~/Scripts/jquery.uploadify.js
  • ~/Content/UploadifyContent/uploadify.swf
  • ~/Content/UploadifyContent/uploadify-cancel.png
  • ~/Content/UploadifyContent/uploadify.css

Example of View

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Upload Files</title>
    @Scripts.Render("~/bundles/jquery")
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.uploadify.js")"></script>       
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/UploadifyContent/uploadify.css")" />

    <script type="text/javascript">
        $(function () {
            $('#file_upload').uploadify({
                'swf': "@Url.Content("~/Content/UploadifyContent/uploadify.swf")",
                'cancelImg': "@Url.Content("~/Content/UploadifyContent/uploadify-cancel.png")",
                'uploader': "@Url.Action("Upload", "Home")",
                'onUploadSuccess' : function(file, data, response) {
                     $("#uploaded").append("<img src='" + data + "' alt='Uploaded Image' />");
                }
            });
        });
    </script>

</head>
<body>
    <div>
        Click Select files to upload files.
        <input type="file" name="file_upload" id="file_upload" />
    </div>
    <div id="uploaded">
    </div>
</body>
</html>

Example of Controller (Controllers Homecontroller.Cs)

public ActionResult Index()
{
    return View();
}

public ActionResult Upload()
{
    var file = Request.Files["Filedata"];
    string savePath = Server.MapPath(@"~\Content\" + file.FileName);
    file.SaveAs(savePath);

    return Content(Url.Content(@"~\Content\" + file.FileName));
}
  • I did all this and nothing... I can’t install the package because the jQuery version used in the package is too old and accurate for the version I use in this project. ?

  • @Luiznegrini I updated the answer. See now.

  • Still nothing appears the way you said... suddenly it would be more interesting to implement in the hand... just know that nothing appears when I click the button to confirm at the end of the form.

  • great, but every photo I add it rejects. Give the 404 in the description.

  • @Luiznegrini I forgot to say that the methods are implemented in HomeController for this example. I will change the answer again.

  • Problem solved! I just had to change the controller in the script and it was hehe! I just wanted to ask a question, it is good practice to use Request.Files or change the signature of the method to "Httpfilebase"?

  • @Luiznegrini The coolest is you use public HttpPostedFileBase Arquivo { get; set; } in the Model. Then the file becomes one property much easier to be manipulated.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.