4

I made the following request to download a file from Sharepoint 2013 using REST api:

GET https://mydomain.sharepoint.com/mysite/_api/web/GetFolderByServerRelativeUrl('/mysite/testlib')/files('test-file.pdf')/$value
Accept-Encoding: gzip
Accept: application/octet-stream
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ...
User-Agent: Google-HTTP-Java-Client/1.13.1-beta (gzip)

I got the response with correct file's content, but the Content-Type of the response is:

Content-Type: application

Is that anyway I can make Sharepoint returns full Mime-Type in the response header? i.e. Content-Type: application/pdf

Thanks in advance!

2 Answers 2

2

You have to add binaryStringResponseBody: true,

var info = {
        url: appwebUrl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('/sites/Test/Shared Documents/" + sourcefile.get_name() + "')/$value?@target='myhosturl'",

        binaryStringResponseBody: true,
        success: function (data) {
        },
        error: function () { alert('error'); }

    }

in msdn, you can find binaryStringRequestBody: true for adding file to Document library. Similarly you can add binaryStringResponseBody: true to download a file. Hope This helps.

0

I was unable to download the file as it seemed to be ignoring my HTTP Header (as per @sekhar's answer).

Instead, once I got the filename I downloaded it directly (rather than via the REST API). Here's my code:

$date = (Get-Date).AddDays(-1).ToString("yyyy-MM-ddT00:00:00")

$baseURL = "http://server/site"
$libName = "LibName"

$restURL = "$baseURL/_api/Web/lists/GetByTitle('$libName')"
$filterUrl = "$restURL/items?`$filter=Modified ge datetime'$date'"

$libURL = "$baseURL/$libName"
$filePath = "F:\DownloadTemp"

$request = [System.Net.WebRequest]::Create($filterUrl)
$request.Method = "GET"
$request.UseDefaultCredentials = $true

$resp = $request.GetResponse()
$reader = new-object System.IO.StreamReader($resp.GetResponseStream())
[xml]$xml = $reader.ReadToEnd()

$ns = @{
         d="http://schemas.microsoft.com/ado/2007/08/dataservices"
     m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
       }

$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true

$xml | Select-Xml '//d:ID' -Namespace $ns | foreach-object {

        $fileInfoURL = "$restURL/items($_)/file"

        $request = [System.Net.WebRequest]::Create($fileInfoURL)
        $request.Method = "GET"
        $request.UseDefaultCredentials = $true

        $resp = $request.GetResponse()
        $reader = new-object System.IO.StreamReader($resp.GetResponseStream())
        [xml]$xml2 = $reader.ReadToEnd()

        $xml2 | Select-Xml '//m:properties/d:Name' -Namespace $ns  | foreach-object {

            $libURLFile = "$libURL/$_"
            #$libURLFile

            $filePath = "$filePath\$_"
            #$filePath 

            $webclient.DownloadFile($libURLFile,$filePath)  

            #DO WORK

            #Remove-Item $filePath
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.