DCSIMG
Download file in asp.net - שלמה גולדברג (הרב דוטנט)

שלמה גולדברג (הרב דוטנט)

מרצה בסלע ויועץ בעולם ה - net.

Download file in asp.net

 

בתפוז נשאלה שאלה:
 
מה הקוד שצריך לכתוב כדי להוריד קובץ בעזרת לינק פשוט ?
 
אני נתקל הרבה פעמים בשאלה הזאת - ולכן אני כותב כאן את דוגמת הקוד.
 
 
צריך לייצר handler ולכתוב ב - ProcessRequest את הקוד הבא:
 

public void ProcessRequest(HttpContext context)

{

    string fileName = context.Request.QueryString["filename"];

    FileInfo fi = new FileInfo(fileName);

    context.Response.ContentType = "application/x-rar-compressed";

 

    context.Response.AppendHeader("Content-Disposition",

        string.Format("attachment; filename=download{0}", fi.Name));

 

    context.Response.WriteFile(fileName);

    context.Response.End();

}

 
כמובן שה - ContentType צריך להיות לפי סוג הקובץ (אפשר לבדוק בעזרת המאפיין  Extension של FileInfo.
 
 
קוד ה - html
 

<a href="MyHandler.ashx?filename=C:\MyFile.rar">Download</a>

תוכן התגובה

Shlomo כתב/ה:

חיפוש קצר בגוגל מה ה - ContentType של סוג הקובץ ותבדוק את הסיומת של הקובץ ולפי זה תכתוב את ה - ContentType

# October 2, 2009 11:04 AM

Moshe L כתב/ה:

אפשר גם application/msdownload

או application/x-octet-stream

ויש גם משתנה נוסף ששייך ל-Content Dispotion שנקרא attachment, מה שמאפשר "הורדה" גם של קבצי jpg ואחרים שהדפדפן אמור להציג בדרך כלל.

# October 3, 2009 6:26 PM

Shlomo כתב/ה:

תודה

# October 3, 2009 9:11 PM

TCDooM כתב/ה:

Just remember this is asking for a hacked website! never Ever let a client side parameter be a path to file on the local drive!

# October 4, 2009 3:34 PM

תקוה כתב/ה:

יש גם פטנט איך לגרום לקובץ לרדת בחלקים, וכך קובץ גדול לא תוקע את המחשב להרבה זמן:

    // Buffer to read 10K bytes in chunk:

   byte[] buffer = new Byte[10000];

   // Length of the file:

   int length;

   // Total bytes to read:

   long dataToRead;

   FileStream oMovie = null;

   try

   {

     oMovie = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

     // Total bytes to read:

     dataToRead = oMovie.Length;

     switch (sContentType)

     {

       case "wmv":

         Response.ContentType = "video/x-ms-wmv";

         break;

.

.

{

  Response.AddHeader("Content-Disposition", "attachment; filename=" + movieName);

     // Read the bytes.

     while (dataToRead > 0)

     {

       // Verify that the client is connected.

       if (Response.IsClientConnected)

       {

         // Read the data in buffer.

         length = oMovie.Read(buffer, 0, 10000);

         // Write the data to the current output stream.

         Response.OutputStream.Write(buffer, 0, length);

         // Flush the data to the HTML output.

         Response.Flush();

         buffer = new Byte[10000];

         dataToRead = dataToRead - length;

       }

       else

       {

         //prevent infinite loop if user disconnects

         dataToRead = -1;

       }

     }

catch (Exception ex)

   {

     Console.WriteLine(ex.Message);

     Response.Write("Cannot find " + movieName);

     return;

   }

   finally

   {

     if (oMovie != null)

     {

       //Close the file.

       oMovie.Close();

     }

     Response.Close();

   }

# October 14, 2009 10:19 PM

Citizenship & Programming » ???????? ?????????? כתב/ה:

Pingback from  Citizenship &amp; Programming &raquo; ???????? ??????????

# October 14, 2009 10:38 PM

יוסי גולדברג כתב/ה:

לא הבנתי למה צריך handler?

זה הקוד ששמתי ב page load:

Response.AppendHeader("Content-Disposition",

           string.Format("attachment; filename=download{0}", "myFile.css"));

וזה עובד כמו שצריך.

אגב ה content type לא ממש משנה לו.

# October 29, 2009 1:55 PM

Shlomo כתב/ה:

אתה צודק זה יעבוד גם בדף - אבל למה צריך דף עם זה עובד ב - handler ?

כשיש דף יש לנו תהליך יקר של "מחזור החיים של דף" שעולה הרבה (יחסית ל - handler) לשרת

# October 29, 2009 2:50 PM

שמואל כתב/ה:

תודה רבה, הצלת לי פרויקט של שלושה חודשים!

# November 22, 2009 8:00 PM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 1 and 1 and type the answer here:


Enter the numbers above: