Upload files use ajax jquery or normal post method in mvc razor -Inside Modal Window - Not working

Posted by Krishnasamy2008 under ASP.NET MVC on 6/24/2014 | Points: 10 | Views : 1694 | Status : [Member] | Replies : 1
Approach 2:Index.cshtml

@(Html.Kendo().Window()
.Name("MWUploadData")
.Title("Upload Data")
.Content(@<text>
<table style="width: 100%;" >

<tr align="center" >

<td colspan="2"> Bulk Data Upload</td>
</tr>
<tr>
<td>Enter Dataset:
</td>
<td>
<input type="file" id="fileUpload" name="fileUpload"/>



</td>
</tr>
<tr>
<td colspan="2" align="center"> <input type="button" value="Upload" id="hl-start-upload" class="k-button search" onclick="return SaveUploadData();" /> &nbsp;&nbsp;
<input type="button" value="Cancel" class="k-button k-button-icontext k-grid-Copy"
style="background-color: #228B22; color: White;" onclick="return CancelUploadData();" />
</td>

</tr>

</table>



</text>)
.Draggable()
.Resizable()
.Width(600)
.Height(400)
.Modal(true)
.Visible(false)
)


Approach2::Controller.cs:
---------------------------
[HttpPost]

public ActionResult UploadDataToServer(HttpPostedFileBase postedfile)
{
HttpPostedFileBase myFile = Request.Files["fileUpload"];
bool isUploaded = false;
string message = "File upload failed";

if (myFile != null && myFile.ContentLength != 0)
{
string pathForSaving = Server.MapPath("~/Uploads");
if (this.CreateFolderIfNeeded(pathForSaving))
{
try
{
myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName));
isUploaded = true;
message = "File uploaded successfully!";
}
catch (Exception ex)
{
message = string.Format("File upload failed: {0}", ex.Message);
}
}
}
return Json(new { isUploaded = isUploaded, message = message }, "text/html");
}


Approach2:Jquery: - It is not supporting file:$('#fileUpload').prop("files")[0] in jquery
----------------------------------
<script type="text/javascript">

function SaveUploadData() {
debugger;
// var formData = new FormData();
// formData.append("coverFile", $("#fileUpload").file);

alert("step1");
//var file = $('#fileUpload').prop("files")[0];

$.ajax({
type: 'POST',
//url: '@Url.Action("UploadDataToServer")',
url: "/DataCorrection/UploadDataToServer",
dataType: 'json',
//contentType: "multipart/form-data",
//data: { Postedfile: $('#fileUpload').prop("files")[0] },
data: { studyID: $.trim($("#txtStudyID").val()), fdaNumber: $.trim($("#txtFDANumber").val()),file:$('#fileUpload').prop("files")[0]},
success: function (empList) {
//debugger;
//alert(empList);
alert('success');
},
error: function (ex) {





Responses

Posted by: Vuyiswamb on: 7/6/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
What Errors do you get ?

Thank you for posting at Dotnetfunda
[Administrator]

Krishnasamy2008, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response