2017-07-25 98 views
0

我使用此處聲明的代碼通過webapi http://bartwullems.blogspot.pe/2013/03/web-api-file-upload-set-filename.html上傳文件。我還做了以下api列出我擁有的所有文件:已上傳文件但未在服務器上顯示

[HttpPost] 
    [Route("sharepoint/imageBrowser/listFiles")] 
    [SharePointContextFilter] 
    public async Task<HttpResponseMessage> Read() 
    { 
     string pathImages = HttpContext.Current.Server.MapPath("~/Content/images"); 

     DirectoryInfo d = new DirectoryInfo(pathImages);//Assuming Test is your Folder 
     FileInfo[] Files = d.GetFiles(); //Getting Text files 
     List<object> lst = new List<object>(); 
     foreach (FileInfo f in Files) 
     { 
      lst.Add(new 
      { 
       name = f.Name, 
       type = "f", 
       size = f.Length 
      }); 
     } 
     return Request.CreateResponse(HttpStatusCode.OK, lst); 

    } 

當調用此API時,會列出上傳的所有文件。但是,當我去湛藍的我沒有看到任何人的(Content.png是我手動上傳到Azure中的文件) enter image description here

爲什麼這些文件,如果他們不出現在蔚藍上市。

回答

1

根據您的描述,我建議您可以先使用azure kudu控制檯在azure門戶網站中找到正確的文件夾以查看圖像文件。

打開捻控制檯:

enter image description here

在捻點擊調試控制檯並找到網站\ wwwroot的\ yourfilefolder

enter image description here

如果你發現你的文件仍沒有按」上傳成功,我猜可能你的上傳代碼有問題。我建議你可以嘗試下面的代碼。

注意:您需要在wwwort文件夾中添加圖像文件夾。

{ 
    public class UploadingController : ApiController 
    { 
     public async Task<HttpResponseMessage> PostFile() 
     { 
      // Check if the request contains multipart/form-data. 
      if (!Request.Content.IsMimeMultipartContent()) 
      { 
       throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
      } 
      string root = Environment.GetEnvironmentVariable("HOME").ToString() + "\\site\\wwwroot\\images"; 
      //string root = HttpContext.Current.Server.MapPath("~/images"); 
      var provider = new FilenameMultipartFormDataStreamProvider(root); 

      try 
      { 
       StringBuilder sb = new StringBuilder(); // Holds the response body 

       // Read the form data and return an async task. 
       await Request.Content.ReadAsMultipartAsync(provider); 

       // This illustrates how to get the form data. 
       foreach (var key in provider.FormData.AllKeys) 
       { 
        foreach (var val in provider.FormData.GetValues(key)) 
        { 
         sb.Append(string.Format("{0}: {1}\n", key, val)); 
        } 
       } 

       // This illustrates how to get the file names for uploaded files. 
       foreach (var file in provider.FileData) 
       { 
        FileInfo fileInfo = new FileInfo(file.LocalFileName); 
        sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length)); 
       } 
       return new HttpResponseMessage() 
       { 
        Content = new StringContent(sb.ToString()) 
       }; 
      } 
      catch (System.Exception e) 
      { 
       return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
      } 
     } 
    } 

    public class FilenameMultipartFormDataStreamProvider : MultipartFormDataStreamProvider 
    { 
     public FilenameMultipartFormDataStreamProvider(string path) : base(path) 
     { 
     } 

     public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers) 
     { 
      var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : Guid.NewGuid().ToString(); 
      return name.Replace("\"", string.Empty); 
     } 
    } 


} 

結果:

enter image description here

+0

感謝!不知道這個KUDU。這些文件在那裏,不知道爲什麼他們不出現在VS服務器窗口。 – Flezcano

相關問題