Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Create download link call web api.

$
0
0

Create download link in which file is on server.Write Web Api Lokk Like this But it does not work.

[AllowAnonymous]
        [Route("GetApp")]
        [HttpGet]
        public HttpResponseMessage GetApp(string AppId)
        {


            var app = DbContext.Apps.FirstOrDefault(a => a.ID == new Guid(AppId));

           string appPath = @"C:\inetpub\Dev-QuickAppsBuilder\Apps\" + app.User.Id + "\\" + app.ID;
          // string appPath = @"C:\Users\appideas-users\Documents\Visual Studio 2013\Projects\AppBuilder\AppBuilder.Web\AppBuilder.Web\Apps\7a29740a-b75b-459f-b6bf-3a96804f00f9\aa207cb3-b57f-e511-8259-bc5ff4d5634f";

            string zipPath = "appzip/" + app.User.Id.ToString() + "/" + app.ID ;

            EnsureDirectoryExist(zipPath);
            try
            {
               System.IO.Compression.ZipFile.CreateFromDirectory(appPath,MapPath(zipPath)+".zip", CompressionLevel.Fastest, true);
            }
            catch (Exception ex)
            {
                System.IO.File.Delete(MapPath(zipPath) + ".zip");
                System.IO.Compression.ZipFile.CreateFromDirectory(appPath, MapPath(zipPath) + ".zip", CompressionLevel.Fastest, true);
            }



            FileProvider = new FileProvider(app.User.Id);
            FileStream fileStream = FileProvider.Open(app.ID + ".zip");
            var response = new HttpResponseMessage();
            response.Content = new StreamContent(fileStream);
            response.Content.Headers.ContentDisposition
                = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = app.ID + ".zip";
            response.Content.Headers.ContentType
                = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentLength
                    = FileProvider.GetLength(app.ID + ".zip");
            return response;

           // return Ok(response);
        }
 static public string MapPath(string value)
        {
            if (value[0] == '/' || value[0] == '\\')
            {
                value = value.Remove(0, 1);
            }
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            var hostingPath = Directory.GetParent(Path.GetDirectoryName(path)).FullName;
            var combined = Path.Combine(hostingPath, value);
            return combined;
        }

        void EnsureDirectoryExist(string path)
        {
            var target = MapPath(path);
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
        }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>