SORU
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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Jucyber Tutoriais

    Jucyber Tuto

    8 EYLÜL 2009
  • max2sims2

    max2sims2

    19 Kasım 2008
  • tychoadragmire

    tychoadragmi

    20 Mart 2006