Skip to content

Commit 76c478e

Browse files
authored
Merge pull request #153 from aleixoporpino/master
C# example
2 parents f047a80 + ccc239f commit 76c478e

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

c#/Home/Index.cshtml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@*
2+
* jQuery Upload File Plugin C# Implementation Example
3+
* Developed by Aleixo Porpino Filho
4+
*@
5+
6+
@{
7+
ViewBag.Title = "Home Page";
8+
Layout = null;
9+
}
10+
11+
<link href="https://rawgithub.com/hayageek/jquery-upload-file/master/css/uploadfile.css" rel="stylesheet">
12+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
13+
<script src="~/Content/jquery-upload-file-master/js/jquery.uploadfile.js"></script>
14+
15+
<br />
16+
<div class="row">
17+
<div class="col-md-4">
18+
<div id="fileuploader">Upload</div>
19+
</div>
20+
</div>
21+
<div id="extrabutton" class="ajax-file-upload-green">Start Upload</div>
22+
23+
<script>
24+
//This example show how to use upload plugin with Asp.Net C# MVC
25+
//The plugin use an extraHTML, download and delete buttons
26+
$(document).ready(function () {
27+
var extraObj = $("#fileuploader").uploadFile({
28+
url: "../Home/UploadFile",
29+
statusBarWidth: 'auto',
30+
dragdropWidth: 'auto',
31+
showDelete: true,
32+
showDownload: true,
33+
autoSubmit: false,
34+
showProgress: true,
35+
extraHTML: function () {
36+
var html = "<div><b>File Tags:</b><input type='text' name='tags' value='' /> <br/>";
37+
html += "<b>Category</b>:<select name='category'><option value='1'>ONE</option><option value='2'>TWO</option></select>";
38+
html += "</div>";
39+
return html;
40+
},
41+
deleteCallback: function (data, pd) {
42+
$.post("../Home/DeleteFile?url=" + data.url,
43+
function (resp, textStatus, jqXHR) {
44+
//Show Message
45+
console.log(resp, textStatus);
46+
alert(resp.msg);
47+
pd.statusbar.hide(); //You choice.
48+
});
49+
},
50+
downloadCallback: function (json, pd) {
51+
console.log(pd);
52+
window.open('../Home/DownloadFile?url=' + json.url, "_blank");
53+
}
54+
});
55+
$("#extrabutton").click(function () {
56+
extraObj.startUpload();
57+
});
58+
});
59+
</script>

c#/HomeController.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using System.Web;
4+
using System.Web.Mvc;
5+
6+
namespace jQuery_Upload_File_Plugin_DotNet.Controllers
7+
{
8+
/*
9+
* C# implementation example
10+
* Developed by Aleixo Porpino Filho
11+
*/
12+
public class HomeController : Controller
13+
{
14+
public ActionResult Index()
15+
{
16+
return View();
17+
}
18+
19+
[HttpPost]
20+
public ActionResult UploadFile()
21+
{
22+
try
23+
{
24+
HttpPostedFileBase hpf = HttpContext.Request.Files["file"] as HttpPostedFileBase;
25+
string tag = HttpContext.Request.Params["tags"];// The same param name that you put in extrahtml if you have some.
26+
string category = HttpContext.Request.Params["category"];
27+
28+
DirectoryInfo di = Directory.CreateDirectory(Server.MapPath("~/Tmp/Files"));// If you don't have the folder yet, you need to create.
29+
string savedFileName = Path.Combine(di.FullName, hpf.FileName);
30+
hpf.SaveAs(savedFileName);
31+
32+
var msg = new { msg = "File Uploaded", filename = hpf.FileName, url= savedFileName };
33+
return Json(msg);
34+
}
35+
catch (Exception e)
36+
{
37+
//If you want this working with a custom error you need to change in file jquery.uploadfile.js, the name of
38+
//variable customErrorKeyStr in line 85, from jquery-upload-file-error to jquery_upload_file_error
39+
var msg = new { jquery_upload_file_error = e.Message };
40+
return Json(msg);
41+
}
42+
}
43+
[Route("{url}")]
44+
public ActionResult DownloadFile(string url)
45+
{
46+
return File(url, "application/pdf");
47+
}
48+
49+
[HttpPost]
50+
public ActionResult DeleteFile(string url)
51+
{
52+
try
53+
{
54+
System.IO.File.Delete(url);
55+
var msg = new { msg = "File Deleted!" };
56+
return Json(msg);
57+
}
58+
catch (Exception e)
59+
{
60+
//If you want this working with a custom error you need to change the name of
61+
//variable customErrorKeyStr in line 85, from jquery-upload-file-error to jquery_upload_file_error
62+
var msg = new { jquery_upload_file_error = e.Message };
63+
return Json(msg);
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)