我想知道是否可以检测浏览器是否在iOS上运行,类似于使用Modernizr进行特征检测的方式(尽管这显然是设备检测而不是功能检测).
通常我更喜欢功能检测,但我需要找出一个设备是否是iOS,因为他们根据这个问题处理视频的方式YouTube API无法使用iPad/iPhone /非Flash设备
我的API控制器正在返回一个csv文件,如下所示:
[HttpPost]
public HttpResponseMessage GenerateCSV(FieldParameters fieldParams)
{
var output = new byte[] { };
if (fieldParams!= null)
{
using (var stream = new MemoryStream())
{
this.SerializeSetting(fieldParams, stream);
stream.Flush();
output = stream.ToArray();
}
}
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(output) };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "File.csv"
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
我发送和接收csv文件的angularjs如下所示:
$scope.save = function () {
var csvInput= extractDetails();
// File is an angular resource. We call its save method …
Run Code Online (Sandbox Code Playgroud) 我想在浏览器窗口中打开Blob对象.
这个代码可以在任何地方工作,但iOS Chrome(和IE当然,但我可以解决IE).窗口未重定向到URL(这是正确的或至少与其他浏览器相同).Chrome iOS有任何已知的解决方法吗?
var blob = new window.Blob(['Hello, world!'], {type: 'text/plain;charset=utf-8'});
window.URL = window.URL || window.webkitURL;
var url = window.URL.createObjectURL(blob);
window.location.href = url;
Run Code Online (Sandbox Code Playgroud)
我试过<a href="{blobUrl}>
而不是window.location.href
但它也不起作用.
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return Download();
}
public FileResult Download()
{
string xmlString = "my test xml data";
string fileName = "test" + ".xml";
return File(Encoding.UTF8.GetBytes(xmlString), "application/xml", fileName);
}
}
Run Code Online (Sandbox Code Playgroud)
我在asp.net mvc应用程序中有上面的代码来下载文件.它工作正常,因为我的控制器继承到Controller.但是,当我将此代码移动到Webapi控制器时,它会在返回File时抛出错误.经过分析,我发现webapi中的控制器继承到ApiController(system.web.http.api控制器).我发现ApiController中没有File类.有没有选项在webapi控制器中实现下载文件功能?
我在webapi控制器中尝试了下面的替代代码,但是一旦我调用它就看不到下载文件.
public HttpResponseMessage DownloadConstructedXmlFile()
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
string xmlContent = "My test xml data";
//var serializer = new XmlSerializer(typeof(xmlContent));
var builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
// serializer.Serialize(writer, xmlContent); …
Run Code Online (Sandbox Code Playgroud) 将文件上传到服务器时,它很棒; 没有损坏的文件.然而,当我下载文件时(除了纯粹的txt:s,它们是woork),它们的大小会变大并变得腐败.经过大量的调查后,我不知道会出现什么问题.我只是将文件作为流写入响应并下载blob.
欢迎任何想法!
严重依赖此Thread解决方案; 使用AngularJS从ASP.NET Web API方法下载文件
现行守则如下;
的WebAPI:
[Route("GetFile")]
public HttpResponseMessage GetFile()
{
HttpResponseMessage result = null;
//Get file object here
try
{
IEnumerable<string> headerValues = Request.Headers.GetValues("fileID");
int key = Int32.Parse(headerValues.FirstOrDefault());
var fetchFile = db.FileRecords.Single(a => a.id == key);
var localFilePath = fetchFile.path + fetchFile.name;
if (!System.IO.File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
//I have used the x-filename header to send the filename. This is a custom header for convenience.
//You should …
Run Code Online (Sandbox Code Playgroud) 我想使用从Web服务器下载pdf文件$http
.我使用这个效果很好的代码,我的文件只保存为html文件,但是当我打开它时,它会以pdf格式打开但在浏览器中.我在Chrome 36,Firefox 31和Opera 23上进行了测试.
这是我的angularjs代码(基于此代码):
UserService.downloadInvoice(hash).success(function (data, status, headers) {
var filename,
octetStreamMime = "application/octet-stream",
contentType;
// Get the headers
headers = headers();
if (!filename) {
filename = headers["x-filename"] || 'invoice.pdf';
}
// Determine the content type from the header or default to "application/octet-stream"
contentType = headers["content-type"] || octetStreamMime;
if (navigator.msSaveBlob) {
var blob = new Blob([data], { type: contentType });
navigator.msSaveBlob(blob, filename);
} else {
var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL; …
Run Code Online (Sandbox Code Playgroud) 我根据斯科特的答案撰写了一份指令.你会这样使用它:
<button class="btn btn-success"
download-response="getData()"
download-success="getDataSuccess()"
download-error="getDataError()"
download-name="{{name}}.pdf"
download-backup-url="/Backup/File.pdf">
Save
</button>
Run Code Online (Sandbox Code Playgroud)
问题:下面的代码引发错误TypeError: Invalid calling object
在IE11
第一方法(行:saveBlob(blob, filename);
).即使它回归到其他下载方法,我的理解是saveMethod1应该工作IE11
.
这是代码:
'use strict';
// directive allows to provide a function to be executed to get data to be downloaded
// attributes:
// download-response - Required. Function to get data. It must return a promise. It must be declared on the $scope.
// download-success - Optional. Function to be executed if download-response function was …
Run Code Online (Sandbox Code Playgroud) 我试图从Web API获取PDF文档,并希望在Angular App中显示.获取"无法加载PDF文档错误".我在" 角度应用程序 "帖子中跟随了" AngularJS:显示blob(.pdf) ".然而,我可以通过以下" 使用AngularJS从ASP.NET Web API方法下载文件 "文章成功下载相同的文件.
看起来我得到的文件是"Chunked Transfer Encoded".不知何故,当试图在角度应用程序中显示时,这不会被解码.请指教.
Web API代码:
HttpResponseMessage result = null;
var localFilePath = @"C:\Test.pdf";
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition.FileName = "Test.pdf";
result.Content.Headers.Add("x-filename", "Test.pdf");
}
return result;
Run Code Online (Sandbox Code Playgroud)
角度控制器:
myModule.controller("pdfviewerController", function ($scope, $http, $log, $sce) {
$http.post('/api/Sample/GetTestFile', {responseType:'arraybuffer'})
.success(function (response) {
var file …
Run Code Online (Sandbox Code Playgroud) angularjs ×6
javascript ×5
c# ×2
ios ×2
.net ×1
asp.net-mvc ×1
blob ×1
browser ×1
csv ×1
download ×1
httpresponse ×1
pdf ×1