i am having problem with .stl file download in c# angular js mvc.
while the file is downloading i am not able to open the file in 3d shape or anyother software supporting stl file extension.
vm.FileDownload = function (id, hash,filename) {
var defer = $q.defer();
threeShapeRepo.getFile(id, hash, filename).then(function (data) {
if (data !== null) {
var file = new Blob([data], {
type: 'application/stl'
});
var fileURL = URL.createObjectURL(file);
var link = document.createElement('a');
link.href = fileURL;
link.target = '_blank';
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
//window.URL.revokeObjectURL(url);
defer.resolve(data);
});
}
From repository.js
function getFile(id, hash, fileName) {
var params = {
'Id': id,
'Hash': hash,
'FileName': fileName
}
var url = 'https://' + window.location.host + '/api/' + 'ThreeShape' + '/' + 'getFileAsync';
return $http.post(url, params, { responseType: 'blob' }).then(function (data) {
if (data.data.Content.indexOf("validation failed")!==-1) {
logError(data.data.Content);
return null;
}
return data.data;
});
}
in c# back end code
public async Task<string> getFileAsyncInternal(GetFile request)
{
var response = new GetThreeShapeCasesResponse();
IList<GetOneThreeShapeCaseResult> tempCaseList = new List<GetOneThreeShapeCaseResult>();
IRestResponse restResponse;
var retryCount = 0;
HttpStatusCode statusCode;
int numericStatusCode;
var requestUrl = "";
InitializeThreeShapeClient();
var restRequest = new RestRequest(Method.GET);
if (request.Hash != null)
{
requestUrl = threeShapeServerLocation + "api/cases/"+ request.Id+"/attachments/"+ request.Hash;
}
ThreeShapeAPIClient.BaseUrl = new Uri(requestUrl);
var getTokenOperation = await retreive3ShapeBearerTokenInternal();
var myToken = getTokenOperation.TokenValue;
var tokenString = "Bearer " + myToken;
restRequest.AddHeader("Authorization", tokenString);
restResponse = ThreeShapeAPIClient.Execute(restRequest);
numericStatusCode = (int)restResponse.StatusCode;
System.Web.HttpResponse httpresponse = System.Web.HttpContext.Current.Response; ;
if (numericStatusCode == 200)
{
var strResult = restResponse.Content;
//return strResult;
string fileName = request.FileName;
httpresponse.ClearContent();
httpresponse.Clear();
byte[] buffer = new byte[restResponse.Content.Length];
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
Inline = true // false = prompt the user for downloading; true = browser to try to show the file inline
};
httpresponse.Headers.Add("Content-Disposition", "attachment;filename="+request.FileName+ ";filename*=UTF-8''" + request.FileName);
httpresponse.Headers.Add("Content-Length",Convert.ToString(restResponse.Content.Length));
httpresponse.Headers.Add("Contenty-Type", ((RestSharp.RestResponseBase)restResponse).ContentType);
httpresponse.Output.Write(restResponse.Content);
return restResponse.Content;
}
else if (retryCount == 0 && numericStatusCode == 401)
{
response.DidError = true;
response.ErrorMessage = restResponse.Content;
}
return restResponse.Content;
}
I have been struggling to make the downloaded file open. Any kind of help is deeply appreciated.
Response i am getting in content object is
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…