แสดงบทความที่มีป้ายกำกับ Upload Folder แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Upload Folder แสดงบทความทั้งหมด
วันจันทร์ที่ 18 พฤษภาคม พ.ศ. 2558
Uploading a Folder in Asp.net MVC4
2 ความคิดเห็น:
Posted by
Aagkasit Tontan
at
03:24
Labels:
Asp.net Mvc4,
c#,
Upload Folder,
Uploading a Folder
View
@{
ViewBag.Title = "uploadFolder";
}
upload Folder
ScriptControllor
public ActionResult uploadFolder()
{
return View();
}
[HttpPost]
public async Task UploadHomeReport()
{
try
{
foreach (string file in Request.Files)
{
var fileContent = Request.Files[file];
if (fileContent != null && fileContent.ContentLength > 0)
{
// get a stream
var stream = fileContent.InputStream;
// and optionally write the file to disk
var fileName = fileContent.FileName;
fileName = fileName.Replace(@"/",@"\");
string[] arr = fileName.Split('\\');
//\tculms\sub\sub1
var tempPath = string.Empty;
for (int i = 0; i < arr.Length; i++) {
if (!isFolder(arr[i]))
{
var targetPath = string.Empty;
if (i == 0)
{
targetPath = Path.Combine(Server.MapPath("~/Images"), arr[i]);
tempPath = tempPath + "\\" + arr[i];
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
}
else
{
tempPath = tempPath + "\\" + arr[i];
targetPath = Server.MapPath("~/Images") + tempPath;
//targetPath = Path.Combine(Server.MapPath("~/Images"), tempPath);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
}
}
}
var path = Path.Combine(Server.MapPath("~/Images"), fileName);
using (var fileStream = System.IO.File.Create(path))
{
stream.CopyTo(fileStream);
}
}
}
}
catch (Exception)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Upload failed");
}
return Json("File uploaded successfully");
}
private bool isFolder(string txt) {
var arr = txt.ToCharArray();
for (int i = 0; i < arr.Length;i++ )
{
if (arr[i] == '.') {
return true;
}
}
return false;
}
Web configResult
Derivative
สมัครสมาชิก:
ความคิดเห็น (Atom)