Skip to content

Commit

Permalink
Fixed upload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
madskristensen committed Sep 11, 2017
1 parent 786ff82 commit 835a4b7
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions BlogTemplate/Models/BlogDataStore.cs
Expand Up @@ -356,10 +356,13 @@ public void SaveFiles(List<IFormFile> files)
{
using (Stream uploadedFileStream = file.OpenReadStream())
{
string name = file.FileName;
string name = Path.GetFileName(file.FileName);
string filePath = Path.Combine(UploadsFolder, name);

if(CheckFileNameExists(name))
{
_fileSystem.DeleteFile($"{UploadsFolder}\\{name}");
_fileSystem.DeleteFile(filePath);
continue;
}

byte[] buffer = new byte[1024];
Expand All @@ -371,7 +374,7 @@ public void SaveFiles(List<IFormFile> files)
{
break;
}
_fileSystem.AppendFile($"{UploadsFolder}\\{name}", buffer, 0, bytesRead);
_fileSystem.AppendFile(filePath, buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
Expand All @@ -384,21 +387,9 @@ public IEnumerable<string> GetFileNames()
return fileNames;
}

private bool CheckFileNameExists(string fileName)
{
return _fileSystem.FileExists($"{UploadsFolder}\\{fileName}");
}

private string CreateFileName(string fileName)
private bool CheckFileNameExists(string filePath)
{
string tempName = fileName;
string shortName = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
for(int i = 1; CheckFileNameExists(tempName); i++)
{
tempName = $"{shortName}-{i}{ext}";
}
return tempName;
return _fileSystem.FileExists(filePath);
}
}
}

0 comments on commit 835a4b7

Please sign in to comment.