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

  • Cartoonium

    Cartoonium

    11 NİSAN 2011
  • Manuel Vizcaino

    Manuel Vizca

    27 Mayıs 2008
  • UKF Dubstep

    UKF Dubstep

    29 NİSAN 2009