[英]Upload file to Azure
我想使用C#.net在azure中上传文件,无论它采用哪种格式。 我想检查文件是否存在。 如果存在,则动态创建一个文件夹以保存上载的文件。
如何传递另一个参数,该参数将检查上载的文件是否存在,然后检查是否存在,然后在Uploads
文件夹中创建一个子文件夹进行保存?
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);
}
}
上传文件之前,请检查路径中是否存在相同的文件名。 您可以使用以下代码检查文件是否存在
//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/zh-CN/library/system.io.file.exists%28v=vs.110%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.