Flezcano
Flezcano

Reputation: 1687

Files uploaded but not appearing on server

I use the code stated here to upload files through a webapi http://bartwullems.blogspot.pe/2013/03/web-api-file-upload-set-filename.html. I also made the following api to list all the files I have :

[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);

    }

When calling this api, all the files uploaded are listed. But when I go to azure I dont see any of them (Content.png is a file I manually uploaded to azure) enter image description here

Why are the files listed if they dont appear on azure.

Upvotes: 0

Views: 43

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 28067

According to your description, I suggest you could firstly use azure kudu console to locate the right folder in the azure web portal to see the image file.

Open kudu console:

enter image description here

In the kudu click the debug console and locate the site\wwwroot\yourfilefolder

enter image description here

If you find your file is still doesn't upload successfully, I guess there maybe something wrong with your upload codes. I suggest you could try below codes.

Notice: You need add image folder in the wwwort folder.

{
    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);
        }
    }


}

Result:

enter image description here

Upvotes: 2

Related Questions