"1"
ExceptionMessage:"Ссылка на объект не указывает на экземпляр объекта."
ExceptionType:"System.NullReferenceException"
Message: "An error has occurred."
500 (Internal Server Error)
string root = AppDomain.CurrentDomain.BaseDirectory + "Data\\Storage\\Документы";
HttpResponseMessage result = null;
// проверка существует ли папка Документы
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(root);
var resultData = await Request.Content.ReadAsMultipartAsync(provider);
var data = resultData.FormData["data"];
if (data == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
DocInfo docInfo = JsonConvert.DeserializeObject<DocInfo>(data);
var projectFolder = docInfo.projectFolder;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
// проверка существует ли папка проекта
if (!Directory.Exists(root + "\\" + projectFolder))
{
Directory.CreateDirectory(root + "\\" + projectFolder);
}
if (!Directory.Exists(root + "\\" + projectFolder + "\\" + docInfo.TypeFolder))
{
Directory.CreateDirectory(root + "\\" + projectFolder + "\\" + docInfo.TypeFolder);
}
var postedFile = httpRequest.Files[file];
string date = DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss");
string fileFormat =
Path.GetExtension(root + "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" +
postedFile.FileName);
string pathNew = "";
string pathOld = "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" +
postedFile.FileName;
//var filePath = File.Create("E:\\Storage" + "\\Test\\" + postedFile.FileName);
ProjectDoc doc = new ProjectDoc();
doc.ProjectId = docInfo.ProjectId;
if (docInfo.Name != "" && docInfo.Name != null && docInfo.Name != " ")
{
doc.Name = docInfo.Type + "_" + docInfo.Name + "_" + date;
pathNew = "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" + docInfo.Type +
"_" +
docInfo.Name + "_" + date + fileFormat;
}
else
{
doc.Name = docInfo.Type + "_" + date;
pathNew = "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" + docInfo.Type +
"_" +
docInfo.Name + "_" + date + fileFormat;
}
doc.Format = fileFormat;
doc.Path = pathNew;
doc.Type = docInfo.Type;
doc.TypeFolder = docInfo.TypeFolder;
doc.DateCreate = DateTime.Now;
db.ProjectDocs.Add(doc);
db.SaveChanges();
string filePathOld = root + pathOld;
string filePathNew = root + pathNew;
docfiles.Add(filePathNew);
postedFile.SaveAs(filePathOld);
File.Move(filePathOld, filePathNew);
File.Delete(root + "\\" + postedFile.FileName);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
return result;
var server = HttpContext.Current.Server;
var httpRequest = HttpContext.Current.Request;
string root = server.MapPath("~/Data/Storage/Документы");
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(root);
var resultData = await Request.Content.ReadAsMultipartAsync(provider);
var data = resultData.FormData["data"];
var filesUpload = resultData.FileData;
HttpResponseMessage result = null;
// проверка существует ли папка Документы
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
if (data == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
DocInfo docInfo = JsonConvert.DeserializeObject<DocInfo>(data);
var projectFolder = docInfo.projectFolder;
if (filesUpload.Count > 0)
{
var docfiles = new List<string>();
foreach (var file in filesUpload)
{
// проверка существует ли папка проекта
if (!Directory.Exists(root + "\\" + projectFolder))
{
Directory.CreateDirectory(root + "\\" + projectFolder);
}
if (!Directory.Exists(root + "\\" + projectFolder + "\\" + docInfo.TypeFolder))
{
Directory.CreateDirectory(root + "\\" + projectFolder + "\\" + docInfo.TypeFolder);
}
string date = DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss");
string oldFileName = file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
string fileFormat = Path.GetExtension(root + "\\" + oldFileName);
string pathNew = "";
string pathOld = "\\" + oldFileName;
ProjectDoc doc = new ProjectDoc();
doc.ProjectId = docInfo.ProjectId;
if (docInfo.Name != "" && docInfo.Name != null && docInfo.Name != " ")
{
doc.Name = docInfo.Type + "_" + docInfo.Name + "_" + date;
pathNew = "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" + docInfo.Type +
"_" +
docInfo.Name + "_" + date + fileFormat;
}
else
{
doc.Name = docInfo.Type + "_" + date;
pathNew = "\\" + docInfo.projectFolder + "\\" + docInfo.TypeFolder + "\\" + docInfo.Type +
"_" +
docInfo.Name + "_" + date + fileFormat;
}
doc.Format = fileFormat;
doc.Path = pathNew;
doc.Type = docInfo.Type;
doc.TypeFolder = docInfo.TypeFolder;
doc.DateCreate = DateTime.Now;
db.ProjectDocs.Add(doc);
db.SaveChanges();
string filePathOld = root + pathOld;
string filePathNew = root + pathNew;
docfiles.Add(filePathNew);
File.Copy(filePathOld, filePathNew);
File.Delete(root + "\\" + oldFileName);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return result;