Tag: file upload

覆盖WebHostBufferPolicySelector以进行非缓冲文件上载

在尝试创建非缓冲文件上载时,我扩展了System.Web.Http.WebHost.WebHostBufferPolicySelector,覆盖了本文所述的函数UseBufferedInputStream(): http : //www.strathweb.com/2012/09/dealing -with-large-files-in-asp-net-web-api / 。 当一个文件POST到我的控制器时,我可以在跟踪输出中看到被覆盖的函数UseBufferedInputStream()肯定会按预期返回FALSE。 但是,使用诊断工具,我可以看到内存随着文件上传而增长。 大量内存使用似乎发生在我的自定义MediaTypeFormatter中(类似于FileMediaFormatter: http ://lonetechie.com/)。 在这个格式化程序中,我想逐步将传入的文件写入磁盘,但我还需要解析json并使用Content-Type:multipart / form-data upload执行其他操作。 因此我使用HttpContent方法ReadAsMultiPartAsync(),这似乎是内存增长的来源。 我已经在“await”之前/之后放置了跟踪输出,并且看起来当任务阻塞时,内存使用量会相当快地增加。 一旦我在ReadAsMultiPartAsync()返回的部分中找到文件内容,我就使用Stream.CopyTo()将文件内容写入磁盘。 这按预期写入磁盘,但不幸的是,此时源文件已经在内存中。 有没有人对可能出现的问题有任何想法? 似乎ReadAsMultiPartAsync()正在缓冲整个post数据; 如果这是真的,为什么我们需要var fileStream = await fileContent.ReadAsStreamAsync()来获取文件内容? 是否有另一种方法来完成部分的拆分而不将它们读入内存? 我的MediaTypeFormatter中的代码如下所示: // save the stream so we can seek/read again later Stream stream = await content.ReadAsStreamAsync(); var parts = await content.ReadAsMultipartAsync(); // x.Headers.ContentDisposition.DispositionType.ToLower().Trim() == “form-data” && x.Headers.ContentDisposition.Name.ToLower().Trim() […]

使用asp:FileUpLoad控件上传文件时文件太大

我正在使用asp:FileUpLoad上传我的asp.net c#项目中的文件。 只要文件大小不超过允许的最大值,这一切都可以正常工作。 超过最大值时。 我收到错误“ Internet Explorer cannot display the webpage ”。 问题是try catch块没有捕获错误,所以我不能给user a friendly message ,他们已经删除了允许的大小。 我在搜索网页时看到了这个问题,但我找不到可接受的解决方案。 我会看看其他控件,但我的管理可能不会购买第三方控件。 根据建议ajac的答案,我需要添加此评论。 几个月前我试图加载ajax控件。 一旦我使用ajax控件,我就会收到此编译错误。 错误98“System.Web.UI.ScriptControl”类型在未引用的程序集中定义。 您必须添加对程序集’System.Web.Extensions,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35’的引用。 虽然我添加了’ System.Web.Extensions ‘,但我可以摆脱它。 所以我放弃了Ajax并使用了其他技术。 所以我需要解决这个问题或一个全新的解决方案。

建议在上传时检查文件大小的方法

我正在开发一个支持文件上传的Web应用程序。 我已经熟悉检查服务器端的大小,但我想检查客户端的文件大小。 我知道它的浏览器限制,出于安全原因我们无法访问文件属性。 所以我尝试了swfupload和uploadify组件。 两者都很好,满足需求。 但限制是取决于闪存,所以如果我的最终用户没有安装闪存,那么我最终会遇到问题。 要求他们安装闪存是另一回事,但它的门户网站和用户群非常庞大。 所以我不喜欢让他们安装flash的想法。 今天我遇到了gmail中的文件上传function。 并且在没有安装闪存的情况下在浏览器(IE)中进行了测试 我发现的很有趣。 如果您上传了一个大文件,他们会进行回发并立即返回文件大小过大的消息。 如何实现,如何在不下载整个内容的情况下找到文件的大小。 我相信这必须通过阅读HTTP标头信息来完成。 我对吗? 所以这就是我想要实现的确切function。 如果已经安装了闪存,我可以使用uploadify来检查大小,否则可能会实现谷歌使用的从未听过的技术。 谁能推荐我怎么做? 以前任何人都遇到过类似的问题,你做了什么? 更新: 使用FF安装的Flash上​​传Gmail IE中没有flash

以一种formsMVC4上传多个文件

我正在尝试在一个表单上上传多个图像 @using (Html.BeginForm(“Create”, “AdminRestaurants”, FormMethod.Post, new { enctype = “multipart/form-data” })) { Logo: FOH Logo: BOH Logo: MGM Logo: 我正试图用这个处理控制器上的表单 public ActionResult Create(IEnumerable files, RestaurantModel collection) { if (ViewData.ModelState.IsValid) { } } 目前,控制器上的files签名中没有任何内容。 只使用一个文件时,这似乎很有效 public ActionResult Create(HttpPostedFileBase file, EventsModel collection) 有人能指出我允许使用一个提交表单上传多个文件的方向吗?

使用JSON请求和RestSharp通过c#上传文件

在管理通过c#将数据加载到我的Rails服务器之后(请点击此处了解我在说什么),我现在正尝试将文件上传到同一台服务器以及其他数据。 在Ruby中,我可以使用代码执行此操作: require ‘HTTMultiParty’ class ReceiptCreate include HTTMultiParty # Log to file # debug_output File.new(“httparty1.log”, “w+”) base_uri “localhost:3000” format :json headers “Accept” => “application/json” def initialize end def post(machine_serial,filepath,tag_number,token) options = { body: {receipt: {tag_number:tag_number, receipt_file: File.new(filepath), ispaperduplicate:0 }, machine: {serial_number: machine_serial, safe_token: token } } } self.class.post(‘/receipts’, options) end end receipt = ReceiptCreate.new() filename1 […]

MVC3如何检查HttpPostedFileBase是否为图像

我有一个像这样的控制器: public ActionResult Upload (int id, HttpPostedFileBase uploadFile) { …. } 如何确保uploadFile是一个图像(jpg,png等) 我试过了 using (var bitmapImage = new Bitmap (uploadFile.InputStream)) {..} 如果无法创建bitmapImage,则会抛出ArgumentException。 有没有更好的方法,例如通过查看up​​loadFile.FileName?

C# – 上传到服务器后文件已损坏

我使用以下源代码上传文件excel和pdf,但在文件移动到服务器后,文件已损坏。 我认为问题在于编码过程Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); ,但我不知道如何解决它。 public static void sampleUpload() { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://100.165.80.15:21/output/Group Dealer, Main Dealer, Zone, Branch, and Destination Report_20120927105003.pdf”); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential(“toc”, “fid123!!”); // Copy the contents of the file […]

Asyncfileupload文件预览未显示

我在更新面板中有一个Ajax异步文件上传控件。 我的上传工作正常,但上传完成后,我需要看到我上传的图片。 但它不起作用就是我所做的 function UploadComplete(sender, args) { var filename = args.get_fileName(); var contentType = args.get_contentType(); if (contentType.indexOf(‘image’) == -1) { document.getElementById(”).innerText = “Uploaded file must be an Image!”+ “” + args.get_errorMessage() + “”; document.getElementById(”).text.style.backgroundColor = “Red”; } else { var text = “” + filename + ” | ” + args.get_length() + ” bytes”+”Uploaded Succesfully”; […]

WCF REST文件上载

我正在开发一个WCF Web服务,需要能够上传文件等。 目前我添加’floorplan’项的方法如下: [OperationContract] [WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “Floorplan?token={token}&floorplan={floorplan}”)] string XmlInputFloorplan(string token, string floorplan); 我需要更改它,以便将图像作为此调用的一部分上载,可以在以下方法中使用: public static Guid AddFile(byte[] stream, string type); 在这种情况下, byte[]是图像的内容。 然后将得到的guid传递给数据层,并最终确定平面图的添加。 所以我需要弄清楚两件事: 1)我应该如何改变XmlInputFloorplan接口方法,以便它还允许图像作为参数? 2)如何在更改后使用服务? 谢谢! 这是我解决它的方式: [OperationContract] [WebInvoke(Method = “POST”, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = “Floorplan”)] XmlDocument XmlInputFloorplan(Stream content); 期待输入XML,如: Image包含一个base […]

C#调整jpg图像,转换为字节并使用varbinary保存到数据库中

我正在尝试调整我使用FileUpload控件上传的jpg图像,并在将其保存到数据库(SQL Server 2008)之前将其转换为byte(varbinary(MAX))。 我所做的和下面的代码显示我设法将其转换为字节并作为varbinary(MAX)保存到数据库中。 我想知道在完成所有这些function之前如何调整图像大小。 帮帮我。 谢谢! 阅读文件 string filePath = FileUpload1.PostedFile.FileName; string filename = Path.GetFileName(filePath); string ext = Path.GetExtension(filename); 根据文件扩展名设置contenttype string contenttype = String.Empty; switch (ext) { case “.jpg”: contenttype = “image/jpg”; break; } 转换为字节并使用varbinary保存到数据库中 if (contenttype != String.Empty) { Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); //insert […]