Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please i want some sample code for

uploading a file in mvc using Uploadify



Thanks,
Subhranshu
Posted

XML
<link href="~/Uploadify/uploadify.css" rel="stylesheet" />
   <script src="~/Uploadify/jquery-1.4.1.js"></script>
   <script src="~/Uploadify/jquery.uploadify.v2.1.4.min.js"></script>
   <script src="~/Uploadify/swfobject.js"></script>

SQL
this is my controller Class

public ActionResult UploadAsync()
       {<<br mode="hold" />var model = new ListingModel();
List<string> imgNames;
if (Session["images"] == null)
{
imgNames = new List<string>();
}
else
{
imgNames = (List<string>)Session["images"];
}
string name = string.Empty;
if (Request.Files.Count > 0)
{
List<string> exts = new List<string>();
exts.Add(".gif");
exts.Add(".jpg");
exts.Add(".jpeg");
exts.Add(".bmp");
var file = Request.Files[0];
if (exts.Contains(System.IO.Path.GetExtension(file.FileName).ToLower()))
{
Random rnd = new Random();
name = rnd.Next(111, 9999).ToString() + "_" + System.IO.Path.GetFileName(file.FileName);
imgNames.Add(name);
string fullPath = Path.Combine(Server.MapPath("~/Content/AdImages"), name);
file.SaveAs(fullPath);
Session["images"] = imgNames;
}
else
{
name = "error";
}
// model.Images = imgNames;
}

return Json(name, JsonRequestBehavior.AllowGet); 
}



this is the javascript


<script type="text/javascript">
 $(document).ready(function () {
$('#File1').uploadify({
'uploader': '@Url.Content("~/Content/Uploadify/uploadify.swf")',
'script': '@Url.Content("~/Listing/UploadAsync/")',
'cancelImg': '@Url.Content("~/Content/Uploadify/cancel-uploading.png")',
'auto': true,
"buttonText": "Upload Image",
"scriptData": {},
"multi": true,
"onComplete": function (event, ID, fileObj, response, data) {
var json = jQuery.parseJSON(response);
if (json == 'error') {
$("#error").html('extension not supported');
}
else {
// $("#error").html('photo Uploaded');
$("#error").html('');
var imgs = document.getElementById('Images');
var thumbs = document.getElementById('thumbnails');
// imgs.value = json + '~' + imgs.value;
if (imgs.value != null)
imgs.value = imgs.value + '~' + json;
else
imgs.value = json;

var myTr = document.getElementById('trPreview');
var td0 = document.createElement('td');
td0.setAttribute("align", "center");
var img0 = document.createElement('img');
img0.setAttribute("id", json);
var src0 = '@Url.Content("~/Content/AdImages")' + '/' + json;
img0.setAttribute("src", src0);
img0.setAttribute("height", "100");
img0.setAttribute("width", "100");
td0.appendChild(img0);
var br0 = document.createElement('br');
td0.appendChild(br0);
myTr.appendChild(td0);
// var br = document.createElement('br');
// myTr.appendChild(br);
var btn = document.createElement('input');
btn.setAttribute("type", "button");
btn.setAttribute("value", "Remove");
btn.setAttribute("class", "t-button");
var btnId = "btn" + json + "";
btn.setAttribute("id", btnId);
var func = "removeimg('" + json + "','" + btnId + "')";
btn.setAttribute("onclick", func);
td0.appendChild(btn);
myTr.appendChild(td1);
thumbs.appendChild(tbl1);

}

}
});
if ($("#File1Queue") != null) {
$("#File1Queue").hide();
}
}); 
</script></string></string></string></string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900