简体   繁体   English

将文件上传到Azure

[英]Upload file to Azure

I want to upload a file in azure using C#.net no matter what format it will be. 我想使用C#.net在azure中上传文件,无论它采用哪种格式。 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? 如何传递另一个参数,该参数将检查上载的文件是否存在,然后检查是否存在,然后在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);
    }
}

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 https://msdn.microsoft.com/zh-CN/library/system.io.file.exists%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM