簡體   English   中英

將文件上傳到Azure

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM