Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:36:26+00:00 2026-06-02T17:36:26+00:00

I am trying to write a controller that creates and makes a HttpWebRequest to

  • 0

I am trying to write a controller that creates and makes a HttpWebRequest to a service that returns an Image. I then want to return this image as a FileResult. How do I go about doing that. I have tried the code below but it returns a corrupted image instead of the full image:

public FileResult SomeAction()
{
    var request = Make some request here

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        string contentType = response.ContentType;
        return File(response.GetResponseStream(),contentType);
    }
}

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:36:27+00:00Added an answer on June 2, 2026 at 5:36 pm

    Since you fail to mention how “it doesn’t work”, all I can tell you is that in general what you’re doing should work. However, i’m unsure of where the response stream is actually read. Since you’re placing a return inside a using, it may be that the stream is being disposed of before the stream is actually read. Try removing the using statement, and just doing something like this:

    var response = request.GetResponse();
    File(response.GetResponseStream(), response.ContentType);
    

    if that doesn’t work, then verify that response is actually returning a content type, and a valid stream.

    EDIT:

    It would seem that I was correct, the filestream gets closed before it’s read if you wrap it in a using. You don’t have to worry about disposing of the stream as File will dispose of it when it’s done with it.

    See this other question:

    How do I dispose my filestream when implementing a file download in ASP.NET?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a unit test for my one controller to verify
I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a code at the moment that basically tests to see if
I'm trying to write a test for an UrlHelper extensionmethod that is used like
I am trying to dynamically display an image that I am storing in the
I'm trying to write something that will allow a user to use a keyboard/mouse
I'm trying to write a dynamic control in Microsoft MVC that lets me edit
I am trying to write an MVC webpage that has two drop down lists.
Hi I am trying to write just a quick demo service. I am following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.