簡體   English   中英

使用.NET在Autodesk Forge上下載文件

[英]Download a file on Autodesk Forge using .NET

我不確定如何在存儲桶中下載對象。 與存儲桶中上傳的文件相比,我當前能夠下載的文件的大小要小得多。 另外,下載后,我無法打開文件。 我的代碼中缺少什么嗎? 以下代碼是我用來下載文件的代碼。

var element = document.createElement('a');

element.setAttribute('href', '#');
element.setAttribute('download', node.text);
element.style.display = 'none';

document.body.appendChild(element);
element.click();
document.body.removeChild(element);

您在此處引用我的答案( 使用Javascript下載BIM360 Docs文件 )以從Forge OSS存儲桶下載文件。

在此建議中,我擴展了jQuery函數以創建新的XMLHttpRequest並將所有接收到的數據傳遞回jQuery。

/**
 *
 * jquery.binarytransport.js
 *
 * @description. jQuery ajax transport for making binary data type requests.
 * @version 1.0 
 * @author Henry Algus <henryalgus@gmail.com>
 *
 */
// use this transport for "binary" data type
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR) {
    // check for conditions and support for blob / arraybuffer response type
    if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) {
        return {
            // create new XMLHttpRequest
            send: function(headers, callback) {
                // setup all variables
                var xhr = new XMLHttpRequest(),
                    url = options.url,
                    type = options.type,
                    async = options.async || true,
                    // blob or arraybuffer. Default is blob
                    dataType = options.responseType || "blob",
                    data = options.data || null,
                    username = options.username || null,
                    password = options.password || null;

                xhr.addEventListener('load', function() {
                    var data = {};
                    data[options.dataType] = xhr.response;
                    // make callback and send data
                    callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
                });

                xhr.open(type, url, async, username, password);

                // setup custom headers
                for (var i in headers) {
                    xhr.setRequestHeader(i, headers[i]);
                }

                xhr.responseType = dataType;
                xhr.send(data);
            },
            abort: function() {
                jqXHR.abort();
            }
        };
    }
}); 

之后,您只需將filenamebucketKeyYOUR_ACCESS_TOKEN值替換為您自己的值, bucketKey直接在網站上下載文件。 但是,這可能非常不安全,請在此處查看評論

$(function() {

  $('a#download').click(function(event) {
    event.preventDefault();

    const filename = 'hose.rvt';
    const bucketKey = 'adn-test';

    const settings = {
      crossDomain: true,
      url: 'https://developer.api.autodesk.com/oss/v2/buckets/' + bucketKey + ' /objects/' + filename,
      method: 'GET',
      dataType: 'binary',
      processData: false,
      headers: {
        Authorization: 'Bearer YOUR_ACCESS_TOKEN',
        Content-Type: 'application/octet-stream'
      }
    };

    $.ajax(settings).done(function (blob, textStatus, jqXHR) {
        console.log(blob );
        console.log(textStatus);

      if( navigator.msSaveBlob )
        return navigator.msSaveBlob(blob, filename);

      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.style = 'display: none';
      document.body.appendChild(a);

      a.href = url;
      a.download = filename;
      a.click();
      URL.revokeObjectURL(url);
    });
  });
}) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM