Reputation: 9
I want to upload a file in azure using C#.net no matter what format it will be. I want to check whether the file exists or not. If it exists then create a folder dynamically to save the uploaded file.
How can I pass another parameter which will check if the uploaded file exists or not, then if it exists, then create a subfolder in the Uploads
folder to save it?
public UploadedFile Upload(Stream Uploading)
{
try
{
string filename = Guid.NewGuid().ToString() + ".png";
string FilePath = Path.Combine(HostingEnvironment.MapPath("~/Uploads"), filename);
UploadedFile upload = new UploadedFile
{
FilePath = FilePath
};
int length = 0;
using (FileStream writer = new FileStream(upload.FilePath, FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = Uploading.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
upload.FileLength = length;
return new UploadedFile { FilePath = "/Uploads/" + filename };
}
catch (Exception ex)
{
_logger.Error("Error Occured in Upload", ex);
ErrorData errorData = new ErrorData("Error Occured ", ex.Message);
throw new WebFaultException<ErrorData>(errorData, HttpStatusCode.OK);
}
}
Upvotes: 0
Views: 243
Reputation:
Before you upload the file check if the same file name exists in the path. You can check if file exists using below code
//Read the path of your file
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
https://msdn.microsoft.com/en-us/library/system.io.file.exists%28v=vs.110%29.aspx
Upvotes: 1