簡體   English   中英

使用C#將文件上傳到Azure文件存儲中后,目錄0中其他所有文件的長度為

[英]Uploading a file to azure file storage using C# makes all the rest of the files in the directory 0 length

我正在使用以下方法將文件上傳到Azure文件存儲。 它的作用是有時會擦除目錄中已有的文件,將其長度設置為0。新文件的上傳長度正確,而舊文件的長度設置為0。它執行10次中的8次,而沒有拋出任何錯誤。

  public static bool UploadFile(string fileName, byte[] fileInBytes, string directory, string sunDirectory)
    {

        if (fileInBytes == null && fileInBytes.Length == 0)
        {
            Logger.Log("file is zero length");
            return false;
        }

        CloudStorageAccount storageAccount;

        try
        {
            storageAccount = CloudStorageAccount.Parse(documentsConnectionString);
        }
        catch(Exception ex)
        {
            Logger.Log("can't use storage account! " + ex.Message);
            return false;
        }

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        CloudFileShare share = fileClient.GetShareReference(fileShareReference);

        if (share.Exists())
        {
            try
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                CloudFileDirectory directoryRef;

                directoryRef = rootDir.GetDirectoryReference(directory);

                directoryRef.CreateIfNotExists();

                CloudFileDirectory subDirectoryRef;
                subDirectoryRef = directoryRef.GetDirectoryReference(subDirectory);


                subDirectoryRef.CreateIfNotExists();

                CloudFile file = subDirectoryRef.GetFileReference(fileName);

                file.UploadFromByteArray(fileInBytes, 0, fileInBytes.Count());

                return true;
            }
            catch(Exception ex)
            {
                Logger.Log("Uploading file to azure exception: " + ex.Message);
                return false;
            }                
        }
        else
        {
            Logger.Log("Document share does not exist!");
            return false;
        }
    }

我發現了問題。

此代碼

 if (fileInBytes == null && fileInBytes.Length == 0)
    {

必須

 if (fileInBytes == null || fileInBytes.Length == 0)
    {

否則,允許使用零長度的文件進行保存。

多虧大家花時間去看問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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