locked
Return Type to use in a method and launch attachment from the browser RRS feed

  • Question

  • User1793652459 posted

    Hello,


    I have a method that gets the file details and then I call that method in a WebAPI.  I want to launch a file in the browser when users click on a file image.  File could be .doc, xls, pdf etc so everything is dynamic.  I am having a hard time trying to understand how would I pass the file details to the WebAPI.

    Here is the method:

    public string GetFile(int fileId)
    {
    var listValues = GetFileList(fileId);
    byte[] data = null;
    string fileName = string.Empty;
    string contentType = string.Empty;
    for (var i = 0; i < listValues.Count; i++)
    {
    data = listValues[i].FileData;
    fileName = listValues[i].FileName;
    contentType = listValues[i].ContentType ;
    }


    return <not sure what goes here> //Need to pass data, filename and contentType


    }

    Then in this WebAPI I would like to call the method listed above to use 3 file attributes(filename, bytes, contentType) to launch the attachment:

    [HttpGet, Route("{fileId}/DLFile")]
    public IHttpActionResult GetFile(int fileId)
    {

    var dFile = _Rep.GetFile(fileId)); // Received from the method above
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(dFile.bytes);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentType = new MediaTypeHeaderValue(dFile.ConentType);
    response.Content.Headers.ContentDisposition.FileName = dFile.FileName;

    return Ok(response);

    }


    Is this is the right way to launch an attachment from a browser?

    Thanks

    Thursday, October 19, 2017 4:09 PM

Answers

  • User475983607 posted

    This loop gets the last item in the listValues collection.  I'm pretty sure that't not what you want.

    for (var i = 0; i < listValues.Count; i++)
    {
      data = listValues[i].FileData;
      fileName = listValues[i].FileName;
      contentType = listValues[i].ContentType ;
    }

    johnzee

    return <not sure what goes here> //Need to pass data, filename and contentType

    Create an object to hold the three values and return a collection of that object.

        public class MyFileInfo
        {
            public byte[] data { get; set; }
            public string fileName { get; set; }
            public string contentType { get; set; }
        }
            public List<MyFileInfo> GetFile(int fileId)
            {
                List<MyFileInfo> results = new List<MyFileInfo>();
                var listValues = GetFileList(fileId);
    
                foreach(var item in listValues)
                {
                    results.Add(new MyFileInfo
                    {
                        data = item.FileData,
                        fileName = item.FileName,
                        contentType = item.ContentType
                    });
                }
    
                return results;
            }


    You might need to base64 encode the byte array.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 19, 2017 6:43 PM

All replies

  • User753101303 posted

    Hi,

    You are calling this from JavaScript client side ? It could be a problem as then JavaScript get the response and won't process the content-disposition.

    Usually you send the browser itself to this location. The browser see the content type, that this is not supposed to replace the current browser window which is left ucnhanged. It triggers instead a download dialog and the user can then save or open the file with the associated application.

    Edit: try just to access this location directly from your browser address bar. You should then see a download dialog.

    Thursday, October 19, 2017 4:25 PM
  • User1793652459 posted

    The RETURN part is what I really need help with,  How do I return  the file attributes and pass them to the WebAPI?  Thanks

    public string GetFile(int fileId)
    {
    var listValues = GetFileList(fileId);
    byte[] data = null;
    string fileName = string.Empty;
    string contentType = string.Empty;
    for (var i = 0; i < listValues.Count; i++)
    {
    data = listValues[i].FileData;
    fileName = listValues[i].FileName;
    contentType = listValues[i].ContentType ;
    }


    return <not sure what goes here> //Need to pass data, filename and contentType


    }

    Thursday, October 19, 2017 6:23 PM
  • User475983607 posted

    This loop gets the last item in the listValues collection.  I'm pretty sure that't not what you want.

    for (var i = 0; i < listValues.Count; i++)
    {
      data = listValues[i].FileData;
      fileName = listValues[i].FileName;
      contentType = listValues[i].ContentType ;
    }

    johnzee

    return <not sure what goes here> //Need to pass data, filename and contentType

    Create an object to hold the three values and return a collection of that object.

        public class MyFileInfo
        {
            public byte[] data { get; set; }
            public string fileName { get; set; }
            public string contentType { get; set; }
        }
            public List<MyFileInfo> GetFile(int fileId)
            {
                List<MyFileInfo> results = new List<MyFileInfo>();
                var listValues = GetFileList(fileId);
    
                foreach(var item in listValues)
                {
                    results.Add(new MyFileInfo
                    {
                        data = item.FileData,
                        fileName = item.FileName,
                        contentType = item.ContentType
                    });
                }
    
                return results;
            }


    You might need to base64 encode the byte array.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 19, 2017 6:43 PM