I want to upload multiple files from the various user and saves them all at server with individual folder for each user,which contains list of uploaded files by him.
Requirement:If user uploads 5 files,all five files will be stored within a single folder with GUID FolderName.
My below code creates a new folder for each files upload by a user. Ex:If user uploads 5 files,all five files will be stored within five different folder with GUID FolderName.
My Controller Upload method is as shown below:
##Controller Code##
public string Upload(HttpPostedFileBase fileData)
{ var testfolder = this.Server.MapPath("~/uploads/");
string newGuid = Guid.NewGuid().ToString();
string pathToCreate = Path.Combine(testfolder, newGuid);
if (!System.IO.Directory.Exists(pathToCreate))
{
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(testfolder, newGuid));
}
var fileName = pathToCreate + "//" + System.IO.Path.GetFileName(file.FileName);
file.SaveAs(fileName);
return "OK";
}
##View Code##
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="@Href("~/Content/uploadify.css")" rel="stylesheet" />
<link href="@Href("~/Content/bootstrap.css")" rel="stylesheet" type="text/css" />
<div class="row">
<div style="width: 50%;">
<input type="file" id="multipleFiles" style="width: 50%;" /><div style="width: 77%;
float: right" id="progressbar">
</div>
</div>
<div id="fileQueue"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery-1.9.1.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/bootstrap.js")" type="text/javascript"></script>
<script src="@Href("~/Scripts/jquery.uploadify.js")" type="text/javascript"> </script>
<script type="text/javascript">
$(function () {
$("#multipleFiles").uploadify({
'swf': 'Scripts/uploadify.swf',
'cancelImg': '/Images/trash.jpg',
'buttonText': 'Add Documents',
'uploader': 'Upload/Upload',
'folder': '/uploads',
'dataType': 'json',
'fileDesc': 'Image Files',
'fileExt': '', //'*.jpg;*.jpeg;*.gif;*.png',
'multi': true,
'removeCompleted': false,
'auto': false,
'height': '30px',
'queueID': 'fileQueue'
});
});
</script>
Ok, given that you can't use Sessions, I propose the following:
Generate the GUID in the view and include the url as part of the url you are posting to:
Warning: this code comes with no guarantee, I have not tested it so may need some adjustment. Please consider the idea behind it to be more important. :)
View Code:
$(function () {
var guid= '@(Guid.NewGuid().ToString())'; //generated on render time
$("#multipleFiles").uploadify({
'swf': 'Scripts/uploadify.swf',
'cancelImg': '/Images/trash.jpg',
'buttonText': 'Add Documents',
'uploader': 'Upload/Upload?guid='+guid, //modified to include guid
'folder': '/uploads',
'dataType': 'json',
'fileDesc': 'Image Files',
'fileExt': '', //'*.jpg;*.jpeg;*.gif;*.png',
'multi': true,
'removeCompleted': false,
'auto': false,
'height': '30px',
'queueID': 'fileQueue'
});
});
Controller Code:
public string Upload(HttpPostedFileBase fileData, Guid guid) {
//now you can use the guid to save the files
}