locked
Save image instead of displaying it in web project RRS feed

  • Question

  • I have a web project with a lot of content.

    Now when a user wants to download an image  he clicks on download button and he will  be redirected to the url of that content . So he will have displayed the image and can save it by right click....

    What I want is when the user clicks on download  to directly save the content and not display it and have the option "save as" or something like that.

    I need this also to work for mobiles.

    I tried with webClient.DownloadFile but it just saves the image without giving information about it.

    Friday, September 20, 2013 7:24 AM

All replies

  • Hello adrinanadrianp,

    try to this:

    when you click on download button get that url & using httpresponce or downloadoperation. For downloadOperation:

    private DownloadOperation _activedownload;
    
    StorageFile file = await Package.Current.InstalledLocation.CreateFileAsync("imagename.png",CreationCollisionOption.ReplaceExisting);
    BackgroundDownloader downloader = new BackgroundDownloader();
    
                                DownloadOperation operation = downloader.CreateDownload(new Uri("Your image url"), file);
    
     await StartDownloadAsync(download);
    
    private async Task StartDownloadAsync(DownloadOperation downloadOperation)
    {
        DownloadButton.IsEnabled = false;
        _activeDownload = downloadOperation;
        var progress = new Progress<DownloadOperation>(ProgressCallback);
        await downloadOperation.StartAsync().AsTask(progress);
    }
    
    private void ProgressCallback(DownloadOperation obj)
    {
        double progress 
            = ((double) obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive);
        DownloadProgress.Value = progress * 100;
        if(progress >= 1.0)
        {
            _activeDownload = null;
            DownloadButton.IsEnabled = true;
        }
    }

    For httpresponce:

    Uri uri = new Uri("your image url");
            var fileName = Guid.NewGuid().ToString() + ".jpg";
    
            // download pic
            var bitmapImage = new BitmapImage();
            var httpClient = new HttpClient();
            var httpResponse = await httpClient.GetAsync(uri);
            byte[] b = await httpResponse.Content.ReadAsByteArrayAsync();
    
            // create a new in memory stream and datawriter
            using (var stream = new InMemoryRandomAccessStream())
            {
                using (DataWriter dw = new DataWriter(stream))
                {
                    // write the raw bytes and store
                    dw.WriteBytes(b);
                    await dw.StoreAsync();
    
                    // set the image source
                    stream.Seek(0);
                    bitmapImage.SetSource(stream);
    
                    // set image in first control
                    Image1.Source = bitmapImage;
    
                    // write to pictures library
                    var storageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
                        fileName, 
                        CreationCollisionOption.ReplaceExisting);
    
                    using (var storageStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await RandomAccessStream.CopyAndCloseAsync(stream.GetInputStreamAt(0), storageStream.GetOutputStreamAt(0));
                    }
                }
            }
    • Edited by Khant Nipun Friday, September 20, 2013 9:14 AM
    Friday, September 20, 2013 9:09 AM
  • Are you sure you posted in the correct forum?  This forum is for Windows Store apps, not Web Projects. 

    Regardless, you need to use the Content-Disposition header on the server-side app to indicate that the image should be downloaded by the client, not displayed.  You can Bing the header to find out more.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, September 24, 2013 6:01 PM
    Moderator
  • I have used Content--Disposition header and it works but i have 2 projects and in one of them the download page where i have the download button is inside an iframe.

    In this case the download will not start even if the event is fired. I guess this is because of the iframe

    Friday, September 27, 2013 8:17 AM