ASP.NET上传的文件 [英] ASP.NET Uploaded File

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

问题描述

我有一个客户端上使用jQuery的ASP.NET Web应用程序。
论网络形式之一,我有一大堆的管制,以填补,并上传控制。
用户可以在表单上填写其他条目运行上传。
当用户保存的形式,所有的数据,包括文件存储到数据库中。
问题是,当用户填写表单如何处理上传的文件吗?
我有两个可能的解决方案,但不知道其中哪些是更优化的

I have an ASP.NET web application that uses jQuery on client side. On one of the web forms I have a bunch of controls to fill, and an upload control. User can run upload while filling other entries on the form. When user saves the form, all data including file is stored into database . The question is what to do with uploaded file while user fills the form? I have two possible solutions, but don't know which of them is more optimal


  1. 缓存上传的文件,后来,在保存,检索并存储到数据库中的所有其他数据。

  2. 保存在临时文件夹中的文件,然后读取它。

什么是我们的最佳解决方案?
先谢谢了。

What is the best solution for this? Thanks in advance.

推荐答案

我认为最合适的解决方案将存储在缓存中上传的文件。
这里是code

I think the most appropriate solution will be storing uploaded file in cache. Here is the code

    var fileKey = Guid.NewGuid();
    var fileStream = new Byte[Request.Files[0].ContentLength];
    Request.Files[0].InputStream.Read(fileStream, 0, Request.Files[0].ContentLength);
    Cache[fileKey.ToString()] = fileStream;

的FileKey的GUID可以被存储在视图状态,或发送作为响应于客户端。
后来,当整体形式将被保存,缓存的文件进行检索和与其它数据存储到数据库中。
这个方法的好处是,如果用户从页面导航的缓存文件将到期,从而避免资源泛滥。
我们可以通过设置过期时间

The fileKey GUID can be stored in ViewState, or sent as the response to client. Later, when whole form will be saved, cached file can be retrieved and stored into database with other data. The good thing about this method is that if user navigates from the page cached file will expire, thus avoiding resource flooding. We can set expiration time using

Cache.Add(fileKey, fileStream, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);

功能。

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

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