0

In my MVC application am allowing the user to upload PDF file and uploaded file gets saved in the folder. the file is getting uploaded correctly,but its not getting saved in the folder ... View code is:

<a class="upload" onclick="upload(this);">

function upload(box) {
       var box = dhtmlx.modalbox({
           title: "Upload File",
           text: "<div id='form_in_box'><div>Choose a PDF file to upload <hr/><label>Enter the URL <input type='file' name='file' id='file' style='width: 400px; height: 27px;'></label><br></div><div><span class='dhtmlx_button'><input type='submit' value='Upload File' style='width: 86px' onclick='save_file(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>",
           width: "300px"
       });
   }


function save_file(box) {
       var filename = $("#filename").val();
       if (filename == "") {
           alert("Enter the URL");
           return false;
       }
       dhtmlx.modalbox.hide(box);
       dhtmlx.message("Uploading the file");
       $.post("/FileUpload/UploadURL",
       { filename: '' + filename + '' });
   }

Controller code is:

public ActionResult UploadURL(FormCollection data)
    {
        var filename=data["filename"];
      SaveNewFile(filename);
        return View();
    }
 public ActionResult SaveNewFile(string file)
    {

        var supportedType = new[] { "pdf" };
        var fileExt = System.IO.Path.GetExtension(file).Substring(1);
        var filename = Path.GetFileNameWithoutExtension(file) ?? "";

        if (file.Length > 0 && supportedType.Contains(fileExt))
        {

            string filePath = Path.Combine(HttpContext.Server.MapPath(_fileUploadPath),
                                           Path.GetFileName(file));
            if (!System.IO.File.Exists(filePath))
            {   

                filePath = Server.MapPath(_fileUploadPath + file);
                TempData["UploadValidationMessage_Success"] = "File upload Succeeded.";
                return View();
            }

            else
            {
                TempData["UploadValidationMessage_Failure"] = "File already exist.";
                return View();
            }
        }
        else
        {

            TempData["UploadValidationMessage_Failure"] = "Only PDF files are supported. Try again...";
            return View();
        }
    }
  • In this case if i use "HttpPostedFileBase" class am getting an error..i.e. file=null –  Mar 18 '13 at 04:59

4 Answers4

1

You are not saving it. Just see the below post for how to save file:

File upload in MVC

For a complete tutorial: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

Community
  • 1
  • 1
Falaque
  • 886
  • 2
  • 12
  • 27
0

Use HttpPostedFileBase uploadFile parameter to accept Uploading file and SaveAs(filePath); to save!

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), 
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

Also change your JQuery Post to Jquery Ajax post

$('form').submit(function(event) {
                    event.preventDefault();
                    var file = $("#filename").val();
                    file = file.serialize();
                    $.ajax({  
                        type: "POST",
                        contentType:attr( "enctype", "multipart/form-data" ),
                        url: "/FileUpload/UploadURL",  
                        data: file,  
                        success: function( data )  
                        {  
                            alert( data );  
                        }  
                    });  

                    return false;  
                }  
 </script>
Vitthal
  • 546
  • 3
  • 18
  • if i use HttpPostedFileBase uploadFile am getting an error i.e. uploadFile=null –  Mar 18 '13 at 04:49
0

where in your code are you actually saving the file??? Try making use of

"HttpPostedFileBase" class.

Here's the sample Code

Community
  • 1
  • 1
MrClan
  • 6,402
  • 8
  • 28
  • 43
0

first you need to tell the EncType for the form . Without encType file will not be posted to the server

<form action="" method="post" enctype="multipart/form-data">
</form>

for razor

@using (Html.BeginForm("Index", "yourCOntroller", FormMethod.POST, new { enctype = "multipart/form-data" }))
{
  // some stuff
}
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83