PDA

View Full Version : سوال: ساخت لینک مخصوص دانلود



goldax
چهارشنبه 10 آبان 1391, 13:45 عصر
با سلام خدمت اساتید محترم
آیا کسی اطلاع داره که چطور میشه لینک ها رو ساخت که وقتی کاربر روی اون کلیک می کنه پنجره مربوط به ذخیره (save dialog) باز شه و کاربر بتونه فایل مورد نظر رو ذخیر کنه؟
مثل gmail که 2 نوع لینک واسه بعضی فایل ها مثل فایل های عکس قرار می ده ، یه دونه لینک معمولی که عکس رو در یک پنجره جدید باز می کنه و یه دونه لینک دیگه که وقتی کاربر روش کلیک می کنه پنجره ذخیره کردن باز میشه.
پیشاپیش از راهنمایی شما متشکر هستم

goldax
چهارشنبه 10 آبان 1391, 14:50 عصر
ضمن سلام مجدد

خودم راه حل رو پیدا کردم و اینجا قرار می دم تا سایر دوستان هم اگه نیاز داشتند استفاده کنند:

1- یک عدد LinkButton روی صفحه قرار بدید
2- کد زیر رو در رویداد کلیک LinkButton قرار دهید

String FileName = "نام و پسوندی که می خواهید فایل با آن نام در سیستم کاربر ذخیره شود را اینجا بنویسید";
String FilePath = "آدرس و نام واقعی فایلی که قرار است توسط کاربر دانلود شود را اینجا بنویسید-قانوناً یک فایل روی سرور";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();


توجه :
فراموش نکنید که در خط شش ام نوع فایل را مشخص کنید.

goldax
چهارشنبه 10 آبان 1391, 14:57 عصر
برای مثال:

String FileName = "FileName.txt";
String FilePath = "goldax.txt";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();


فایل goldax.txt را از ریشه سایت برداشته و با نام پیش فرض filename.txt در سیستم کاربر ذخیره می کند. می توانید با استفاده از ساختار guid و متود newguid

System.Guid.NewGuid();

هر بار که کاربر جدیدی قصد دانلود فایل را داشت ، آن را با یک نام جدید روی سیستم کاربر ذخیره کنید.

توجه :
فراموش نکنید که در خط شش ام نوع فایل را مشخص کنید.

goldax
چهارشنبه 10 آبان 1391, 15:16 عصر
لیست نوع فایل های مختلف و mime اونها رو می تونید از اینجا ببینید
http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm

fakhravari
چهارشنبه 10 آبان 1391, 16:06 عصر
یه نمونه
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace Fakhravary.File_Upload.File_Upload.Net
{
public class ThrottledStream : Stream
{
public const long Infinite = 0;

#region Private members
/// <summary>
/// The base stream.
/// </summary>
private Stream _baseStream;

/// <summary>
/// The maximum bytes per second that can be transferred through the base stream.
/// </summary>
private long _maximumBytesPerSecond;

/// <summary>
/// The number of bytes that has been transferred since the last throttle.
/// </summary>
private long _byteCount;

/// <summary>
/// The start time in milliseconds of the last throttle.
/// </summary>
private long _start;
#endregion

#region Properties
/// <summary>
/// Gets the current milliseconds.
/// </summary>
/// <value>The current milliseconds.</value>
protected long CurrentMilliseconds
{
get
{
return Environment.TickCount;
}
}

/// <summary>
/// Gets or sets the maximum bytes per second that can be transferred through the base stream.
/// </summary>
/// <value>The maximum bytes per second.</value>
public long MaximumBytesPerSecond
{
get
{
return _maximumBytesPerSecond;
}
set
{
if (MaximumBytesPerSecond != value)
{
_maximumBytesPerSecond = value;
Reset();
}
}
}

/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// </summary>
/// <returns>true if the stream supports reading; otherwise, false.</returns>
public override bool CanRead
{
get
{
return _baseStream.CanRead;
}
}

/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// </summary>
/// <value></value>
/// <returns>true if the stream supports seeking; otherwise, false.</returns>
public override bool CanSeek
{
get
{
return _baseStream.CanSeek;
}
}

/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// </summary>
/// <value></value>
/// <returns>true if the stream supports writing; otherwise, false.</returns>
public override bool CanWrite
{
get
{
return _baseStream.CanWrite;
}
}

/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
/// <value></value>
/// <returns>A long value representing the length of the stream in bytes.</returns>
/// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
public override long Length
{
get
{
return _baseStream.Length;
}
}

/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
/// <value></value>
/// <returns>The current position within the stream.</returns>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
public override long Position
{
get
{
return _baseStream.Position;
}
set
{
_baseStream.Position = value;
}
}
#endregion

#region Ctor
/// <summary>
/// Initializes a new instance of the <see cref="T:ThrottledStream"/> class with an
/// infinite amount of bytes that can be processed.
/// </summary>
/// <param name="baseStream">The base stream.</param>
public ThrottledStream(Stream baseStream)
: this(baseStream, ThrottledStream.Infinite)
{
// Nothing todo.
}

/// <summary>
/// Initializes a new instance of the <see cref="T:ThrottledStream"/> class.
/// </summary>
/// <param name="baseStream">The base stream.</param>
/// <param name="maximumBytesPerSecond">The maximum bytes per second that can be transferred through the base stream.</param>
/// <exception cref="ArgumentNullException">Thrown when <see cref="baseStream"/> is a null reference.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="maximumBytesPerSecond"/> is a negative value.</exception>
public ThrottledStream(Stream baseStream, long maximumBytesPerSecond)
{
if (baseStream == null)
{
throw new ArgumentNullException("baseStream");
}

if (maximumBytesPerSecond < 0)
{
throw new ArgumentOutOfRangeException("maximumBytesPerSecond",
maximumBytesPerSecond, "The maximum number of bytes per second can't be negatie.");
}

_baseStream = baseStream;
_maximumBytesPerSecond = maximumBytesPerSecond;
_start = CurrentMilliseconds;
_byteCount = 0;
}
#endregion

#region Public methods
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
/// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception>
public override void Flush()
{
_baseStream.Flush();
}

/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
/// </returns>
/// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
/// <exception cref="T:System.NotSupportedException">The base stream does not support reading. </exception>
/// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
public override int Read(byte[] buffer, int offset, int count)
{
Throttle(count);

return _baseStream.Read(buffer, offset, count);
}

/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the origin parameter.</param>
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
/// <returns>
/// The new position within the current stream.
/// </returns>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.NotSupportedException">The base stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
public override long Seek(long offset, SeekOrigin origin)
{
return _baseStream.Seek(offset, origin);
}

/// <summary>
/// Sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
/// <exception cref="T:System.NotSupportedException">The base stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
public override void SetLength(long value)
{
_baseStream.SetLength(value);
}

/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.NotSupportedException">The base stream does not support writing. </exception>
/// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
/// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
/// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count);

_baseStream.Write(buffer, offset, count);
}

/// <summary>
/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
/// </returns>
public override string ToString()
{
return _baseStream.ToString();
}
#endregion

#region Protected methods
/// <summary>
/// Throttles for the specified buffer size in bytes.
/// </summary>
/// <param name="bufferSizeInBytes">The buffer size in bytes.</param>
protected void Throttle(int bufferSizeInBytes)
{
// Make sure the buffer isn't empty.
if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0)
{
return;
}

_byteCount += bufferSizeInBytes;
long elapsedMilliseconds = CurrentMilliseconds - _start;

if (elapsedMilliseconds > 0)
{
// Calculate the current bps.
long bps = _byteCount * 1000L / elapsedMilliseconds;

// If the bps are more then the maximum bps, try to throttle.
if (bps > _maximumBytesPerSecond)
{
// Calculate the time to sleep.
long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond;
int toSleep = (int)(wakeElapsed - elapsedMilliseconds);

if (toSleep > 1)
{
try
{
// The time to sleep is more then a millisecond, so sleep.
Thread.Sleep(toSleep);
}
catch (ThreadAbortException)
{
// Eatup ThreadAbortException.
}

// A sleep has been done, reset.
Reset();
}
}
}
}

/// <summary>
/// Will reset the bytecount to 0 and reset the start time to the current time.
/// </summary>
protected void Reset()
{
long difference = CurrentMilliseconds - _start;

// Only reset counters when a known history is available of more then 1 second.
if (difference > 1000)
{
_byteCount = 0;
_start = CurrentMilliseconds;
}
}
#endregion
}
}

برای استفاده
private const int BufferSize = 8192;
protected void cmdDownload_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}

int kbps = 10000;
long bps = kbps * 1024;

string directory = Server.MapPath("~/");
string filename = Path.Combine(directory, "11.pdf");

FileInfo fileInfo = new FileInfo(filename);

if (!fileInfo.Exists)
{
throw new HttpException(404, "File not found.");
}

Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.AppendHeader("Last-Modified", fileInfo.LastWriteTimeUtc.ToString("r"));
Response.AppendHeader("Content-Type", "application/octet-stream");
Response.AppendHeader("Content-Length", fileInfo.Length.ToString());

using (FileStream sourceStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize))
{
Fakhravary.File_Upload.File_Upload.Net.ThrottledSt ream destinationStream = new Fakhravary.File_Upload.File_Upload.Net.ThrottledSt ream(Response.OutputStream, bps);

byte[] buffer = new byte[BufferSize];
int readCount = sourceStream.Read(buffer, 0, BufferSize);

Response.Buffer = false;

while (readCount > 0)
{
destinationStream.Write(buffer, 0, readCount);
readCount = sourceStream.Read(buffer, 0, BufferSize);
}
}
}

nasr
پنج شنبه 11 آبان 1391, 06:56 صبح
چطور میشه کاری کرد که قبلش دسترسی کاربر چک بشه
مثلا اگه کاربر لاگین کرده بود بتونه دانلود کنه.

ممنون

goldax
پنج شنبه 11 آبان 1391, 10:54 صبح
چطور میشه کاری کرد که قبلش دسترسی کاربر چک بشه
مثلا اگه کاربر لاگین کرده بود بتونه دانلود کنه.

ممنون

یه دونه session یا کوکی واسه لاگین کاربر در نظر بگیر با مقدار true یا false یا ... ، اگه مقدارش true بود بعد کلید دانلود رو براش فعال کن.برای این منظور هیچ وقت نباید آدرس حقیقی یک فایل رو به کاربر نشون بدی.چون اگه یک کاربر آدرس حقیقی فایل رو بفهمه ، می تونه اون رو در اختیار سایرین هم قرار بده و سایرین می تونن بدون لاگین یا حتی بدون سر زدن به سایتت ، با داشتن آدرس فایل و قرار دادنش در نرم افزارهای دانلود ، فایل رو دانلود کنن.