将文件上载到Azure

本文关键字:Azure 上载 文件 | 更新日期: 2023-09-27 18:25:48

我想用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);
    }
}

将文件上载到Azure

上传文件之前,请检查路径中是否存在相同的文件名。您可以使用以下代码检查文件是否存在
//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