上传大文件的 [英] Uploading big Files

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

问题描述

我是一个业余的ASP.net开发人员对我的第一份工作的工作(一个朋友的网站)。 ASP.net 4.0版,使用VS2010。

I am an amateur ASP.net developer working on my first job (a friends website). ASP.net v4.0, using VS2010.

他的公司生产的3D模型(使用三维打印机)。该网站目前正在开发中,但可以在这里找到 。我会是第一个承认,code已经有点冲,并窃取了,但我的朋友与他有什么到目前为止非常高兴。

His company makes 3D models (using a 3D printer). The website is currently in development but can be found here. I would be the first to admit that the code has been a bit rushed and hacked in, but my friend is quite happy with what he has so far.

其中一项要求是,他的客户需要能够上传他们的模型设计文件,这可能是高达100 MB的每一个(或者更多)。我努力让这个才能正常工作。

One of the requirements is that his customers need to be able to upload their model design files, which could be up to 100 MB each (or maybe more). I am struggling to get this to work properly.

我开始使用内置的&LT; ASP:文件上传ID =FileUpload1=服务器/&GT; 标记和动画GIF图像,类似在<一个描述的想法href=\"http://www.asp.net/web-forms/videos/how-do-i/how-do-i-file-uploads-with-an-ajax-style-interface\"相对=nofollow>乔斯塔格纳的教程 - 感谢乔,我喜欢你的presentations很多

I started by using the built in <asp:FileUpload ID="FileUpload1" runat="server" /> tag and an animated gif image, similar to the idea described in Joe Stagner's tutorial - thanks Joe, I like your presentations a lot.

这行工作了一个小的测试文件,但不给上传任何进展迹象。所以,我想利用Sunasara Imdadhusen在他的 code项目文章。我上传code是这样的:

This worked ok for a small test file but doesn't give any indication of upload progress. So I tried to improve my solution using ideas developed from Sunasara Imdadhusen in his code project article. My upload code looks like this:

Task t = Task.Factory.StartNew(() =>
{
    byte[] buffer = new byte[UPLOAD_BUFFER_BYTE_SIZE];

    // Upload the file in chunks so that we can measure how long it is taking.
    using (FileStream fs = new FileStream(Path.Combine(newQuotePath, filename), FileMode.Create))
    {
        DateTime stopwatch = DateTime.Now;

        while (stats.Uploaded < stats.TotalSize)
        {
            int bytecount = postedFile.InputStream.Read(buffer, 0, UPLOAD_BUFFER_BYTE_SIZE);
            fs.Write(buffer, 0, bytecount);

            stats.Uploaded += bytecount;

            double dRate = UPLOAD_BUFFER_BYTE_SIZE / Math.Abs((DateTime.Now - stopwatch).TotalSeconds);
            stats.Rate = (int)(Math.Min(dRate, int.MaxValue));

            // Sleep is for debugging only!
            //System.Threading.Thread.Sleep(2000);

            stopwatch = DateTime.Now;
        }
    }
}, TaskCreationOptions.LongRunning);

其中,统计是存储为一个会话变量,并从在的setInterval运行(javascript函数访问的类的引用...)(而上传正在发生)由PageMethod的:

Where stats is a reference to a class that is stored as a session variable, and is accessed from a javascript function running in a setInterval(...) (while the upload is taking place) by the PageMethod:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static UploadStatus GetFileUploadStatus()
{
    UploadStatus stats = (UploadStatus)HttpContext.Current.Session["UploadFileStatus"];

    if ((stats != null) && (stats.IsReady))
    {
        return stats;
    }
    else
    {
        return null;
    }
}

这工作我的本地机器上(使用线程睡眠减缓上传)。所以我把它发布到我们的主机 123-注册,并如预期没有奏效。 GIF动画来上传启动时,但进度条不动窝。这应该得到尽快禁用的上传的文件输入控制和提交按钮(在iframe)开始采取青睐,成为残疾人。然后该网页只是挂在那里。等了一会后,我点击了页面刷新按钮。当页面刷新,它表明我的测试文件已经成功上传。我尝试了16 KB的文件和一个1.6 MB的文件。

This worked on my local machine (using the thread sleep to slow down the upload). So I published it to our host 123-Reg and it didn't work as expected. The animated gif comes on when the upload starts, but the progress bar doesn't start moving. The file input control and the submit button (in an IFrame) that should get disabled as soon as the upload starts take ages to become disabled. Then the web page just hangs there. After waiting for a while I clicked the page refresh button. When the page refreshed it showed that my test file had been uploaded successfully. I tried a 16 KB file and a 1.6 MB file.

由于这个工作我的本地机器上,我怀疑,这是因为我们的网站主机(123-REG)是使用Web场发生的事情。

Since this worked on my local machine, I suspect that this is happening because our website host (123-Reg) is using a web farm.

无论如何,我认为这是一件容易的事,但它不是,所以我一看一些开源的上传进度条。我看了一下 NeatUpload 但它说:在默认情况下,NeatUpload将不能正常上工作Web园或Web农场,也说要让它上的Web农场工作指定每台服务器的Web.config的部分相同的随机32进制decryptionKey属性。但我不认为我有机会获得123-注册的服务器配置文件(?)。

Anyway I thought this was going to be easy but it is not, and so I had a look for some open-source upload progress bars. I had a look at NeatUpload but it says "By default, NeatUpload won't work properly on a web garden or web farm", and it also says to get it to work on a web farm "Specify the same random 32-hex-digit decryptionKey attribute in the section of each server's Web.config". But I don't think I have access to 123-Reg's server config files(?).

我的朋友曾建议,也许我们可以使用一个服务,如Dropbox的,但我一看,我不知道如何把它添加到我们的网站。这可能是一个不错的选择,因为它们可能会为上传进行优化。

My friend has suggested that perhaps we could use a service such as dropbox, but I had a look and I have no idea how to add this to our website. This might be a good option since they might be optimized for uploads.

任何意见或建议将非常AP preciated - 谢谢

Any advice or suggestions would be very much appreciated - thanks.

编辑:迄今为止的故事......还在为此苦苦

The story so far ... still struggling with this.

我看着使用详细(和其他云存储供应商,如网盘等)的Dropbox。看来,这些服务都是围绕提供与个人用户的存储交互的应用而设计。我希望所有的用户可以将文件上传到我的Dropbox文件夹,但不会共享(客户不应该有机会获得其他客户的设计文件)。无论如何,我进行的,设置一个Dropbox帐户,安装 Sharpbox SDK 并重新规划我的上传code(任务操作里面的位)。这似乎是我的本地机器工作正常,这是一个有点慢,因为它被上传到Dropbox的服务器(我并不需要Thread.sleep代码)。我网站公布的123-注册服务器,并得到了类似的,不可用经验和以前一样。

I looked into using dropbox in detail (and other cloud storage providers such as SkyDrive etc). Seems that these services are designed around providing applications that interact with individual user's storage. I wanted all users to be able to upload files to MY dropbox folder but without sharing (customers should not have access to other customer's design files). Anyway I carried on, setup a dropbox account, installed the Sharpbox SDK and reprogrammed my upload code (the bit inside the task action). This seemed to work ok on my local machine, it was a bit slower because it was uploading to the dropbox server (I didn't need the Thread.Sleep). I published the website to the 123-Reg server and got a similar, unusable experience as before.

到目前为止,我一直在测试IE9,我只是happend与Chrome浏览器尝试一下,我注意到一个有趣的事情。只需点击上传按钮后,但在页面之前刷新浏览器显示,在左下角上传%完成对话框。这种电脑采用了100%,那么我的控制开始显现出一些行动之前整个页面开始再次挂起。

So far I had been testing on IE9, and I just happend to try it out with Chrome and I noticed a funny thing. Just after clicking the upload button but before the page refreshed Chrome showed an upload % complete dialog in the bottom left. This ran to 100%, then my controls started to show some action before the whole page started to hang again.

根据MSDN的 HttpPostedFile
默认情况下,所有的请求,包括表单字段和上传文件,大于256 KB的缓冲到磁盘

According to MSDN the HttpPostedFile: "By default, all requests, including form fields and uploaded files, larger than 256 KB are buffered to disk"

那么,这是否意味着我的工作正在启动的之后的等待上传(这实际上是在客户端和一个缓冲器之间发生的)痛苦的一部分吗?如果是这种情况,那么它是没有意义的,以届时将文件发送到关右收存箱更痛苦?而我的进度条跟踪错位的进展如何?

So does this mean that my Task is being started after the painful part of waiting for the upload (which is actually happening between the client and a buffer)? If this is the case then it would not make sense to make it more painful by then sending the file off to dropbox right? And my progress bar is tracking the progress of the wrong bit?

(今天我要检查我的code的错误处理,因为我有一种感觉,它是挂的原因是因为它没有从异常恢复正常)。这当然感觉就像我被这样学到了很多东西。

(Today I am going to check the error handling of my code since I have a feeling that the reason it is hanging is because it is not recovering from an exception properly). It certainly feels like I am learning a lot by doing this.

推荐答案

这可能是一个有点矫枉过正,但你可以考虑使用BluImp的jQuery的文件上传工具(演示站这里:的 http://blueimp.github.com/jQuery-File-Upload/ ) - 一个名为最大巴甫洛夫开发商修改了原始版本(原本非技术DOTNET),用于在MVC 3

This might be a bit overkill but you could look into using BluImp's jQuery File Upload tool (Demo site here: http://blueimp.github.com/jQuery-File-Upload/ ) - a developer called Max Pavlov has modified the original version (originally for non-dotnet technologies) for use in MVC 3.

它可以在这里找到在git的枢纽 https://github.com/maxpavlov/ jQuery的 - 文件 - Upload.MVC3 的。我已经成功地在这两个MVC 3和MVC 4 Beta版实现这一点。我不得不这样做,使之在MVC 4有效工作的唯一一件事就是删除一些ClientDependancy的(这是处理捆绑和JS和CSS文件微小一个DLL)code,因为这在MVC 4但已经复制功能不MVC 3,此外,我增加了一些页面的维基在GitHub上描述我所做的虽然它没有完成。如果您决定沿着这条路走下去,我可以用我最近发现有更新的一些细节。我的笔记迄今在MVC 4一体化可以在这里找到的https://github.com/maxpavlov/jQuery-File-Upload.MVC3/wiki/MVC-4---EnableDefaultBundles

It can be found here on git hub https://github.com/maxpavlov/jQuery-File-Upload.MVC3 . I have successfully implemented this in both MVC 3 and MVC 4 Beta. The only thing I had to do to make it work effectively in MVC 4 is remove some of the ClientDependancy (This is a DLL that handles bundling and minification of JS and CSS files) code as this replicates functionality already in MVC 4 but not in MVC 3. Additionally I have added some pages to the wiki over on GitHub describing what I did although its not complete. If you decide to go down this route I could update some details there with my more recent findings. My notes so far on MVC 4 integration can be found here https://github.com/maxpavlov/jQuery-File-Upload.MVC3/wiki/MVC-4---EnableDefaultBundles .

顺便说一句我已成功使用这个工具来上传文件高达约2GB和发现它非常灵活!

Incidentally I have managed to upload files up to about 2Gb using this tool and have found it quite flexible!

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

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