1

I am wondering how do I upload files with asp.net mvc 3 and plupload plugin

I tried HttpPostedFileBase but it seems to be always null;

I found this page but I am wondering if there are new ways for asp.net mvc 3.

I am also not 100% sure what is going on this. Is it actually saving the file to the server(App_data) as I do not want this. I am trying to upload icals and then extract the values from the icals and import them into by db.

Then get rid of the file. So I rather not be saving it to the server if necessary.

Community
  • 1
  • 1
Sharpoint
  • 307
  • 1
  • 4
  • 8

2 Answers2

1

Make sure your form contains enctype = "multipart/form-data"

@using (Html.BeginForm("upload", "home", FormMethod.Post,
    new { enctype = "multipart/form-data" }))

And have your input name

<input type="file" name="file" />

The same on the controller

public ActionResult Upload(HttpPostedFileBase file)
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • 1
    right it would be file for parameter. So Does the HttpPostedFileBase wait for all the chunks to arrive? Or will I get incomplete files? I am also wondering is there away to send all the files at once(say if I upload 3 files it sends all 3 at once)? Would I have to disable multiple chuncking or something? I tried to do a collection for HttpPostedFileBase but that did not do anything as I think it is sending them one at a time. – Sharpoint May 02 '11 at 15:59
1

Look here:

$("#uploader").pluploadQueue({
         // General settings
         runtimes: 'silverlight',
         url: '/Home/Upload',
         max_file_size: '10mb',
         chunk_size: '1mb',
         unique_names: true,
         multiple_queues: false,

         // Resize images on clientside if we can
         resize: { width: 320, height: 240, quality: 90 },

         // Specify what files to browse for
         filters: [
            { title: "Image files", extensions: "jpg,gif,png" },
            { title: "Zip files", extensions: "zip" }
        ],

         // Silverlight settings
         silverlight_xap_url: '../../../Scripts/upload/plupload.silverlight.xap'
      });

      // Client side form validation
      $('form').submit(function (e) {
         var uploader = $('#uploader').pluploadQueue();

         // Files in queue upload them first
         if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind('StateChanged', function () {
               if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                  $('form')[0].submit();
               }
            });

            uploader.start();
         } else {
            alert('You must queue at least one file.');
         }

         return false;
      });

And in Controller:

[HttpPost]
public string Upload(  ) {
          HttpPostedFileBase FileData = Request.Files[0];

          if ( FileData.ContentLength > 0 ) {
             var fileName = Path.GetFileName( FileData.FileName );
             var path = Path.Combine( Server.MapPath( "~/Content" ), fileName );
             FileData.SaveAs( path );
          }

          return "Files was uploaded successfully!";
       }

That's all...No chunk is needed in Controller...

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • Chunking is needed if the file is larger than 1mb. .NET will throw `Maximum request length exceeded.` – Piotr Kula May 15 '14 at 19:44
  • why u set it as false `multiple_queues: false,` it has to be true if multiple files need to upload and accordingly server side method code need to be different to handle multiple files. thanks – Thomas Jun 24 '15 at 12:42