I am trying that when a user wants to upload a file, it has to select a directory or had to create a new one. After that it has to browse the file and click on sbmit button to upload it.
But the problem that i am getting is that how i give user option to create new directory when that named directory is not present?
var fileName = "";
var coursesRoot = Server.MapPath("~/App_Data/uploads/");
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
var folder = Request["folder"];
fileSavePath = Path.Combine(coursesRoot, folder, fileName);
// Ensure the path is rooted at CourseSubmissions.
if (!Path.GetFullPath(fileSavePath).StartsWith(coursesRoot)) {
Response.SetStatus(400); // Bad request. The user must have tampered with the folder names on the client
return;
}
can u pls post your solution, im trying to do same thing but i get some errors.
var fileName = "";
var userfolder = Request["userfolder"];
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
var folder = Request["folder"];
fileSavePath = Path.Combine(Server.MapPath("~/Upload/"), folder, userfolder, fileName);
// Ensure the path is rooted at CourseSubmissions.
if (!Path.GetFullPath(fileSavePath).StartsWith(coursesRoot)) {
Response.SetStatus(400); // Bad request. The user must have tampered with the folder names on the client
return;
}
uploadedFile.SaveAs(fileSavePath);
}
nikunj25
Member
288 Points
160 Posts
Create new folder during file Upload
May 24, 2011 10:36 AM|LINK
Hi,
I am trying that when a user wants to upload a file, it has to select a directory or had to create a new one. After that it has to browse the file and click on sbmit button to upload it.
But the problem that i am getting is that how i give user option to create new directory when that named directory is not present?
This is my source code
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Home";
var fileName = "";
var coursesRoot = Server.MapPath("~/App_Data/uploads/");
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
var folder = Request["folder"];
fileSavePath = Path.Combine(coursesRoot, folder, fileName);
// Ensure the path is rooted at CourseSubmissions.
if (!Path.GetFullPath(fileSavePath).StartsWith(coursesRoot)) {
Response.SetStatus(400); // Bad request. The user must have tampered with the folder names on the client
return;
}
uploadedFile.SaveAs(fileSavePath);
}
}
<form method="post" enctype="multipart/form-data" action="">
<fieldset>
<p>
<label for="folder">Directory: </label>
<select name="folder">
@foreach(var folder in Directory.EnumerateDirectories(coursesRoot)){
<option>@folder.Substring(folder.LastIndexOf("\\") + 1)</option>
}
</select>
</p>
<label>Upload Text File</label> @FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:false)
<p>
<p>
<input type="submit" value="Upload" />
</p>
</fieldset>
</form>
@if (IsPost) {
<span>You have sucessfully uploaded <strong>@fileName</strong> to <strong></strong>!</span><br/>
}
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Create new folder during file Upload
May 24, 2011 08:12 PM|LINK
You can use Directory.CreateDirectory to create a new folder.
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(v=VS.100).aspx
If you want to allow the user to choose a name for the directory, add an extra <input type="text" /> to your form.
Web Pages CMS | My Site | Twitter
nikunj25
Member
288 Points
160 Posts
Re: Create new folder during file Upload
May 25, 2011 05:56 AM|LINK
Thanks Mikesdotnetting
dow7
Member
738 Points
452 Posts
Re: Create new folder during file Upload
May 26, 2012 03:20 PM|LINK
Hello,
can u pls post your solution, im trying to do same thing but i get some errors.
var fileName = ""; var userfolder = Request["userfolder"]; if (IsPost) { var fileSavePath = ""; var uploadedFile = Request.Files[0]; fileName = Path.GetFileName(uploadedFile.FileName); var folder = Request["folder"]; fileSavePath = Path.Combine(Server.MapPath("~/Upload/"), folder, userfolder, fileName); // Ensure the path is rooted at CourseSubmissions. if (!Path.GetFullPath(fileSavePath).StartsWith(coursesRoot)) { Response.SetStatus(400); // Bad request. The user must have tampered with the folder names on the client return; } uploadedFile.SaveAs(fileSavePath); }This thread also is part of what im trying to do. http://forums.asp.net/t/1807775.aspx/1?Create+e+New+Folder+Access+Denied+