PDA

View Full Version : مشکل در دانلود لینک ها؟



mosini
شنبه 15 خرداد 1389, 08:38 صبح
سلام دوستان
من وبسایتی دارم که روی IIS محلی خودم با IP valid آپ کردم.کار هم میکنه.ولی مشکلش اینه که اون فایل های (doc و pdf) که برای دانلود گذاشتم کار نمیکنه و خطای روبرو رو میده:
http://www.4shared.com/photo/AYC9KkRK/error.html (http://www.4shared.com/photo/AYC9KkRK/error.html)
توی visual studio هیچ خطایی نمیده و دانلود manager من شروع به دانلود مینکنه ولی روی IIS که میزارم کار نمیکنه.اینو بگم که روی پوشه ای که در WWWROOT قرار دادم مجوز write و read رو گذاشتم باز هم کار نکرد.جالب اینجاست که وقتی نام فایل های دانلود با پسوندشون رو بعد از نام سایت مینویسم ،دانلود شروع میشه.
من برای دانلود از یه httphandler استفاده کردم کد کلاس دانلودم اینه:(البته توی web.config تنظیماتش رو هم انجام دادم)»:فکر میکنید مشکل چیه:(توی فورم http://forums.ASP.NET/p/1565369/3897391.aspx هم گذاشتم،جوابی که دادن خیلی متوجه نشدم)»



using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
/// <summary>
/// Summary description for myDownload
/// </summary>
public class myDownload:IHttpHandler
{
public myDownload()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
try
{
string filename = context.Request.QueryString["file"];
string path = context.Server.MapPath(filename);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".pdf":
type = "Application/pdf";
break;

}
}
context.Response.ContentType = type;
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
string FilePath = context.Server.MapPath(string.Format(@"{0}", filename));
context.Response.WriteFile(FilePath);
context.Response.End();

context.Response.WriteFile(path);
context.Response.End();
}
catch
{
}
//string basePath = context.Server.MapPath("~");
//basePath = Path.Combine(basePath, filename);

//FileStream myStream = new FileStream(basePath, FileMode.Open, FileAccess.Read);
//byte[] buffer = new byte[(int)myStream.Length];
//myStream.Read(buffer, 0, (int)myStream.Length);
//myStream.Close();
//context.Response.Clear();
//context.Response.ContentType = "application/octet-stream";
//context.Response.AddHeader("Content-Length", buffer.Length.ToString());
//context.Response.AddHeader("Content-Disposition", "attachment; filename=" + basePath);
//context.Response.BinaryWrite(buffer);
//context.Response.End();

}

#endregion
}

#Faranak
شنبه 15 خرداد 1389, 11:21 صبح
سلام دوستان
من وبسایتی دارم که روی IIS محلی خودم با IP valid آپ کردم.کار هم میکنه.ولی مشکلش اینه که اون فایل های (doc و pdf) که برای دانلود گذاشتم کار نمیکنه و خطای روبرو رو میده:
http://www.4shared.com/photo/AYC9KkRK/error.html (http://www.4shared.com/photo/AYC9KkRK/error.html)
توی visual studio هیچ خطایی نمیده و دانلود manager من شروع به دانلود مینکنه ولی روی IIS که میزارم کار نمیکنه.اینو بگم که روی پوشه ای که در WWWROOT قرار دادم مجوز write و read رو گذاشتم باز هم کار نکرد.جالب اینجاست که وقتی نام فایل های دانلود با پسوندشون رو بعد از نام سایت مینویسم ،دانلود شروع میشه.
من برای دانلود از یه httphandler استفاده کردم کد کلاس دانلودم اینه:(البته توی web.config تنظیماتش رو هم انجام دادم)»:فکر میکنید مشکل چیه:(توی فورم http://forums.ASP.NET/p/1565369/3897391.aspx هم گذاشتم،جوابی که دادن خیلی متوجه نشدم)»



using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
/// <summary>
/// Summary description for myDownload
/// </summary>
public class myDownload:IHttpHandler
{
public myDownload()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
try
{
string filename = context.Request.QueryString["file"];
string path = context.Server.MapPath(filename);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".pdf":
type = "Application/pdf";
break;

}
}
context.Response.ContentType = type;
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
string FilePath = context.Server.MapPath(string.Format(@"{0}", filename));
context.Response.WriteFile(FilePath);
context.Response.End();

context.Response.WriteFile(path);
context.Response.End();
}
catch
{
}
//string basePath = context.Server.MapPath("~");
//basePath = Path.Combine(basePath, filename);

//FileStream myStream = new FileStream(basePath, FileMode.Open, FileAccess.Read);
//byte[] buffer = new byte[(int)myStream.Length];
//myStream.Read(buffer, 0, (int)myStream.Length);
//myStream.Close();
//context.Response.Clear();
//context.Response.ContentType = "application/octet-stream";
//context.Response.AddHeader("Content-Length", buffer.Length.ToString());
//context.Response.AddHeader("Content-Disposition", "attachment; filename=" + basePath);
//context.Response.BinaryWrite(buffer);
//context.Response.End();

}

#endregion
}



سلام .
دسترسی ها رو تنظیم کنید در فایل Web.config زیر تگ
<system.web>
تگ زیررااضافه کنید:


<identityimpersonate="true"/>


همچنین بعداز بسته شدن تگ athenticationتگ زیررا اضافه کنید:


<authorization>
<denyusers="?"/>
</authorization>


در قسمت permisionsبه یوزر ASP.NET,iisدسترسی fullcontrolبدهید.

اگر مشکل دسترسی نداشته باشید وفقط در آدرس فایل ازشما خطا می گیرد می توانید یک فولدر به نام Filesدر پروزه درست کنید و فایلهایی که می خواهید یوزر بتوان آن هارا دانلود کند درآن قراردهید سپس به صورت زیر عمل کنید:



protected void Page_Load(object sender, EventArgs e)
{
foreach (FileInfo f in new DirectoryInfo(Server.MapPath(Request.ApplicationPa th)+"\\Files").GetFiles())
{
HyperLink lnk = new HyperLink();
lnk.Text = f.Name;
//lnk.NavigateUrl = string.Format("Download.aspx?file={0}",f.FullName);
lnk.NavigateUrl = string.Format("Download.aspx?file={0}", f.Name);
phContent.Controls.Add(lnk);
phContent.Controls.Add(new LiteralControl("<br />"));
}
}



سپس در صفحه ی Download.aspxکدزیررابنویسید:



protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment;filename=" + Request.QueryString["file"]);
//FileStream fs = new FileStream(Request.QueryString["file"], FileMode.Open);
FileStream fs = new FileStream(Server.MapPath(Request.ApplicationPath) +"\\Files\\"+Request.QueryString["file"], FileMode.Open);
BinaryReader r = new BinaryReader(fs);
byte[]b=new byte[r.BaseStream.Length];
r.Read(b, 0, b.Length);
fs.Close();
r.Close();
Response.BinaryWrite(b);
Response.End();
}



این کدبرای دانلود فایل باهرپسوندی به کار می رود.

daffy_duck376
شنبه 15 خرداد 1389, 11:40 صبح
شرمنده اگر مشکلتون حل شد میتونید راجع روشتون یه توضیح کوچیک بدید تا من هم یاد بگیرم