文件上传器加速 [英] File uploader speed up

查看:69
本文介绍了文件上传器加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#在ASP.NET中创建一个文件上传器但是当我上传大文件时需要很长时间才能上传



我如何加快速度?< br $> b $ b

我尝试过:



受保护void Button1_Click(object sender,EventArgs e)
{
if(FileUpload1.HasFiles)
{
foreach(FileUpload1.PostedFiles中的HttpPostedFile postedfile)
{
byte [] img = new byte [postedfile.ContentLength];
postedfile.InputStream.Read(img,0,postedfile.ContentLength);

string file_ext = postingfile.FileName;
string ext = Path.GetExtension(file_ext);

// ftp
if((ext.ToLower()==.jpg)||(ext.ToLower()==。jpeg)||(分机ToLower()==。gif)||(ext.ToLower()==。png))
{
Random rnd = new Random();
int card = rnd.Next();
string location_filename =/ Album /+ Album_Id +/+ card +_+ file_ext;
System.Windows.Forms.MessageBox.Show(location_filename);

MemoryStream stream = new MemoryStream(img);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
if((image.Width> 250&& image.Width< 3650)&&(image.Height> 250)&& image.Height< 4450)
{
ftp_class.UploadFileToFTP(img,location_filename);
}
else
{
// Label1.Text =Image is To Large;

}
// File.WriteAllBytes(Server.MapPath(〜/ Property /)+ Property_ID +/+ card + file_ext,img);
}
}
}





公共课ftp_class 
{
static String ftpusername =Ftp_ExampleCodeExample; //例如username
static String ftppassword =Example @ 123; //例如密码


public static void CreateDirectoryToFTP(string directory)
{
try
{
// Directory.CreateDirectory(System.Web。 Hosting.HostingEnvironment.ApplicationPhysicalPath +/+目录);



FtpWebRequest ftp =(FtpWebRequest)FtpWebRequest.Create(ftp://example.com/+目录);
ftp.Credentials = new NetworkCredential(ftpusername,ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse responseFileDelete =(FtpWebResponse)ftp.GetResponse();

}
catch(Exception ex)
{
// alert_message.alert_message_show(ftp:+ ex);
}
}
public static void UploadFileToFTP(byte [] img,string location_filename)
{
try
{
//文件。 WriteAllBytes(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath +/+ location_filename,img);


FtpWebRequest ftp =(FtpWebRequest)FtpWebRequest.Create(ftp://example.com/+ location_filename);
ftp.Credentials = new NetworkCredential(ftpusername,ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(img,0,img.Length);
ftpstream.Close();

}
catch(exception ex)
{
//extra.alter_box(+ ex);
// alert_message.alert_message_show(ftp:+ ex);
}
}

}

解决方案

获得更快客户端互联网连接。



上传总是慢于下载速度,通常是速度的1/5。大文件占用大量带宽。

检查文件大小,检查上传速度,然后通过计算得到的最佳时间开始

上传速度/(文件字节* 10)==秒转移

- 那''会给你一个近似的最佳案例转移,实际上它几乎可以肯定是比这更慢。



如果您的实际时间相似(即在最佳费率时间和两倍之间),那么您实际上无法在不更改连接的情况下改善上传。


I create a file uploader in ASP.NET using C# but when I upload large file it takes a long time to upload

How I speed it up?

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFiles)
       {
           foreach (HttpPostedFile postedfile in FileUpload1.PostedFiles)
           {
               byte[] img = new byte[postedfile.ContentLength];
               postedfile.InputStream.Read(img, 0, postedfile.ContentLength);

               string file_ext = postedfile.FileName;
               string ext = Path.GetExtension(file_ext);

               // ftp
               if ((ext.ToLower() == ".jpg") || (ext.ToLower() == ".jpeg") || (ext.ToLower() == ".gif") || (ext.ToLower() == ".png"))
               {
                   Random rnd = new Random();
                   int card = rnd.Next();
                   string location_filename = "/Album/" + Album_Id + "/" + card + "_" + file_ext;
                   System.Windows.Forms.MessageBox.Show(location_filename);

                   MemoryStream stream = new MemoryStream(img);
                   System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                   if ((image.Width > 250 && image.Width < 3650) && (image.Height > 250) && image.Height < 4450)
                   {
                       ftp_class.UploadFileToFTP(img, location_filename);
                   }
                   else
                   {
                       // Label1.Text = "Image is To Large";

                   }
                   //    File.WriteAllBytes(Server.MapPath("~/Property/") + Property_ID + "/" + card + file_ext, img);
               }
           }
       }



public class ftp_class
   {
       static String ftpusername = "Ftp_ExampleCodeExample"; // e.g. username
       static String ftppassword = "Example@123"; // e.g. password


       public static void CreateDirectoryToFTP(string directory)
       {
           try
           {
              // Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "/" + directory);



               FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://example.com/" + directory);
               ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
               ftp.KeepAlive = true;
               ftp.UseBinary = true;
               ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
               FtpWebResponse responseFileDelete = (FtpWebResponse)ftp.GetResponse();

           }
           catch (Exception ex)
           {
               //  alert_message.alert_message_show("ftp:" + ex);
           }
       }
       public static void UploadFileToFTP(byte[] img, string location_filename)
       {
           try
           {
             //  File.WriteAllBytes(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "/" + location_filename, img);


               FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://example.com/" + location_filename);
               ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
               ftp.KeepAlive = true;
               ftp.UseBinary = true;
               ftp.Method = WebRequestMethods.Ftp.UploadFile;
               Stream ftpstream = ftp.GetRequestStream();
               ftpstream.Write(img, 0, img.Length);
               ftpstream.Close();

           }
           catch (Exception ex)
           {
               //extra.alter_box("" + ex);
               //   alert_message.alert_message_show("ftp:" + ex);
           }
       }

   }

解决方案

Get a faster client internet connection.

Uploads are always slower than downloads, typically 1/5 the speed at best. And large files take a lot of bandwidth.
Check the size of your file, check your upload speed and start by working out the best time you could get by

upload speed / (file bytes * 10) == seconds to transfer

- that';ll give you an approximate "best case" transfer, in practice it'll almost certainly be slower than that.

If your actual time is similar (i.e. between the best rate time and twice that) then you really can't improve upload without changing your connection.


这篇关于文件上传器加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆