This is a migrated thread and some comments may be shown as answers.

PictureBox and URI Value

1 Answer 499 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Louis
Top achievements
Rank 1
Iron
Iron
Iron
Louis asked on 02 Sep 2019, 04:28 AM

Hello.

I got a PictureBox that the URI is a call to an action method :

="https://mysite.com/Stocks/Dwnlod/" + CStr(Fields.PhysicalName)

 

I use ASP.NET MVC Core and my pictures are not in wwwroot.  They are under another directory of the project.

So I use a method to get the filestream of the file :

[Authorize]
public async Task<FileStreamResult> Dwnlod(string id)
{
    ...
    var memory = new MemoryStream();
    try
    {
        using (var stream = new FileStream(path, FileMode.Open))
        {
            await stream.CopyToAsync(memory);
        }
    }
    catch (Exception e)
    {
 
    }
    memory.Position = 0;
 
    return File(memory, GetContentType(path), Path.GetFileName(path));
}

 

As you can see my action method is decorated [Authorize] so it's impossible to get the stream from the report even if the user is connected and authenticated.  The call of URI from the report seem be anonyous.  So i got a error (of course everything works #1 when I remove the decoration Authorize).

 

Any suggestion to keep [Authorize]?  Can the report can provide some authenification when the URI is called?

 

Thank you all of you at Telerik.

 

 

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 04 Sep 2019, 03:55 PM

Hello Louis,

The reports are generated on the server. In this scenario, the application must have access to the specified resource. In general, when you provide an URL as a PictureBox.Value, we use a WebClient to download the image. At that point, the connection to server fails with the statement that the application's process is not authorized to get the resource.

What actually do you return from Dwnlod method? Is it the report or the picture?

You can test to create a User Function that will download the image after authorizing and returns it (e.g. as type Bitmap) for the picture box value.

The User Function (without authorization) might look as follows:

public static Bitmap GetImage(string url)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create(url);
            System.Net.WebResponse response = request.GetResponse();
            System.IO.Stream responseStream = response.GetResponseStream();
            Bitmap bitmap = new Bitmap(responseStream);
            return bitmap;
        }

 

Note that it will be necessary to authorize the request to successfully download the image from the URL.

 

Regards,
Neli
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Louis
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Neli
Telerik team
Share this question
or