1

I am new to MVC 4 using razor application.

I have a file upload control and submit button which to upload file and submit file to the particular directory (specified in below code) in local computer.

My Requirement:

If file is already exists in that specified directory of file path, i have to ask confirmation whether to over write or create back up of a file .

i am displaying confirmation message as "File already existed, Do you want to overwrite?"

If user clicked

OK/YES ---- I have to create overwrite of a file

CANCEL/NO ---- I have to create backup of a file

I am able to display confirmation message correctly and able to select either OK/YES or

CANCEL/NO .

Note: 1) I should not use model in my application

2) I am using action methods in same controller.

3) I have created "UploadeFiles" folder in my E:\ drive

Problem:

After clicking OK/YES or CANCEL/NO , neither of ajax url's reaching to controller method nor nothing working. I am stuck to here.

Note: But the alert messages corresponding to Overwrite or Backup are working if i click OK/YES or CANCEL/NO

My VIEW:

@if (TempData["FileExists"] != null)
 {
 <script type="text/javascript">
 if (confirm("File Already Existed, Do you want to over write?"))
 {
 alert("Proceed for over writing file");
 var Url = "@Url.Content("~/Documents/OverWrite")";
 $.ajax({
 url: Url,
 type: 'POST',
 dataType: 'json',
 data: { docfileName: "FileData" },
 success: function (data)
 {
 alert("overwrite success")
 }
 });
 } 
 else 
 {
 alert("Create Backup");
 var Url = "@Url.Content("~/Documents/CreateBackup")";
 $.ajax({
 type: 'POST',
 url: Url,
 dataType: 'json',
 data: { docfileName: "FileData"},
 success: function (data) { alert("Backup success") }
 });
 }
 }
</script>
 }
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
 <label style="width: 300%;"for="fileToUpload">Upload File</label>
 <input type="file" name="fileToUpload" id="fileToUpload"/>
 <input type="submit" value="Create" id="btnCreate"/>
}

My CONTROLLER

MVC Controller Name: DocumentsController

public ActionResult CreateDocuments(HttpPostedBaseFile fileToUpload)
{
 string fileName = Path.GetFileName(fileToUpload.FileName);
 string fileToUploadPath= @"E:\UploadFiles\";
 string fileTotalPath = Path.Combine(fileToUploadPath, fileName);
 if (System.IO.File.Exists(fileToUploadPath)
 {
 TempData["FileExists"] = "Yes";
 return PartialView("CreateDocument");
 }
}
[HttpPost]
public ActionResult OverWrite(string docfileName)
 {
 JsonResult resultOverWrite= new JsonResult();
 string ajaxdata=docfileName;
 // Here i am writing code for Overwriting a document file
 return resultOverWrite;
 }
public ActionResult CreateBackup(string docfileName)
 {
 JsonResult resultbackup= new JsonResult();
 string ajaxdata=docfileName;
 // I am writing here for back up coding
 return resultbackup;
 }
tereško
58.5k26 gold badges101 silver badges152 bronze badges
asked Jul 17, 2014 at 15:55

1 Answer 1

2

you are using wrong helper method:

change:

@Url.Content("~/Documents/OverWrite")

to:

@Url.Action("OverWrite","Documents") 
// first parmeter action name, second parameter controller name
answered Jul 17, 2014 at 15:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.