2017-07-25 95 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

相关问题