27 AĞUSTOS 2012, PAZARTESİ
Nasıl indiriliyor dosya adı ayarlamak için Web API, MVC ASP.NET içinde
ApiController sınıfımda, bir dosya sunucu tarafından oluşturulan indirmek için aşağıdaki yöntemi var.
public HttpResponseMessage Get(int id)
{
try
{
string dir = HttpContext.Current.Server.MapPath("~"); //location of the template file
Stream file = new MemoryStream();
Stream result = _service.GetMyForm(id, dir, file);
if (result == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
result.Position = 0;
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
return response;
}
catch (IOException)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
Her şeyi varsayılan dosya indirme kullanıcı iletişim olarak kaydetmek/kendi dosya adı her zaman yazmak zorunda kalabilirsiniz bu yüzden kendi kimliği dışında mükemmel çalışıyor. Kod varsayılan bir dosya adı ayarlamak için herhangi bir yol yukarıda var mı?
CEVAP
27 AĞUSTOS 2012, PAZARTESİ
HttpResponseMessage
üzerine: Content-Disposition
Başlığı ayarlamak gerekir
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "foo.txt"
};
Bunu Paylaş:
Nasıl bir proje içinde tek bir dosya i...
Nasıl kalın yazı tipi stilini ayarlama...
Bu AndroidManifest.xml ayrıştırmak içi...
Nasıl setuptools içinde install_requir...
Nasıl bir hücre genişliğini ayarlamak ...