Tag: file upload

当我上传0个文件时,为什么IEnumerable count为1?

我有一个多上传表单,我想在启动上传时检查是否有任何文件。 这是我的代码。 查看: @using (Html.BeginForm(“Upload”, “Home”, FormMethod.Post, new { enctype = “multipart/form-data”})) { } 控制器: [HttpPost] public ActionResult Upload(IEnumerable files) { if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1 if(files.Any()) Console.WriteLine(files.Any()); // display true if (files.First() == null) Console.WriteLine(“first null”); // display “first null” return View(); } 当我提交空表单时,为什么我的程序会显示结果? 我可能会检查JS我的字段,但我想了解我的IEnumerable这些数据是什么。 谢谢。

WordPress XML RPC上传图片C#

嘿所有,我正在开发一个工作站点,将使用Wordpress XML RPC将信息从数据库推送到Wordpress。 我可以抓取信息并发布它就好了,但是当我到达上传图像时它似乎工作(WP媒体选项卡中没有运行时错误/图像)但是它上传了一个损坏的图像链接。 它似乎不知道从我的图像获取数据,我不知道为什么这里有我的一些代码。 MemoryStream ms = new MemoryStream(); System.Drawing.Image img = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(“_Images/DownloadButton-PSD.png”)); img.Save(ms, ImageFormat.Png); byte[] imagebytes = new byte[ms.Length]; ms.Position = 0; ms.Read(imagebytes, 0, Convert.ToInt32(ms.Length)); 在该代码加载图像信息之后,我将其以Data变量的格式传递给函数 var data = new Data { Base64 = Convert.ToBase64String(imagebytes), Name = “DownloadButton-PSD.png”, Type = “image/png”, Overwrite = false, }; _wpWrapper.UploadFile(data); 仅供参考:我也在使用http://joeblogs.codeplex.com/上的dll作为我的项目 数据类如下所示: public class Data { public […]

SSH.NET:是否可以使用SFTP上传文件并保留源文件中的文件日期?

目前,我使用Renci SSH.NET库使用SFTP将文件上传到Unix服务器。 我不喜欢的一件事是,在上传文件后,创建和修改日期会更改为上传时间。 我想保留源文件中的原始文件日期,这可能吗?

如何将文件上载到IIS中托管的.NET 3.5 WCF服务?

我已经把头发拉了一段时间了。 有人可以提供一个非常简单的示例(或工作示例的链接),说明如何将文件上载到IIS中托管的WCF服务。 我从简单的事情开始。 我想通过POST从客户端调用URL,传递文件名并发送文件。 所以我在合同中添加了以下内容: [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “/UploadFile?fileName={fileName}”)] void Upload(string fileName, Stream stream); 在.svc文件中实现它: public void Upload(string fileName, Stream stream) { Debug.WriteLine((fileName)); } 我立即在运行项目时遇到错误: For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream. 不知道从哪里开始。 很想看到一个真实的工作示例。 PS我在.NET 4中用WCF 4做了这个,它看起来简单得多,但我不得不降级。 在.NET 3.5中,我遗漏了一些东西。

从上传的多个文件中获取文件名

我的表单中有4 fileupload 。 我想当我点击一个按钮,我得到文件名并设置为model,然后将文件上传到服务器。 但我不想要使用4个文件,我可以选择一次使用3个,2个或1个文件上传。 如果我只使用2个文件上传,则file3和file4模型仍然为null。 我必须在我的控制器中做什么? 有什么想法或建议吗? 模型: public string file1 {get;set;} public string file2 {get;set;} public string file3 {get;set;} public string file4 {get;set;} 视图: @using (Html.BeginForm(“action”, “controller”, FormMethod.Post, new { enctype = “multipart/form-data” })) { }

是否有机会使用App_Browsers来检测HTML5 File API的支持?

使用HTML5 File API上传文件,我目前正在使用一些硬编码检查支持它们的浏览器,具体取决于用户代理字符串: internal bool IsHtml5FileUploadCapable { get { var browser = Request.Browser; var n = browser.Browser.ToLowerInvariant(); var major = browser.MajorVersion; var minor = browser.MinorVersion; return n.Contains(@”chrome”) && major >= 6 || n.Contains(@”ie”) && major >= 10 || n.Contains(@”firefox”) && (major >= 3 && minor > 6 || major >= 4) || n.Contains(@”opera”) && (major […]

ASP.NET MVC3从部分视图上载文件(并填充模型中的相应字段)

我知道这个问题已经在SO和其他地方讨论过了 ,但是我找不到任何问题的答案。 我正在开发一个ASP.NET MVC3项目,我想创建一个包含FileUpload的Partial视图 。 这个局部视图在基本的Create页面上调用,我希望上传的文件属于要创建的模型。 只有当用户提交表单时,才会上传所选文件。 以下是代码的解释: Model ModelToCreate public class ModelToCreate { //Some properties public FileUploadModel Files { get; set; } } 模型FileUploadModel public class FileUploadModel { public IEnumerable Files { get; set; } } 我的PartialView(_UploadFiles.cshtml) @model Models.ModelToCreate //I tried with Html.BeginForm(Create, MyController instead of null, null, but with no result. @using (Html.BeginForm(null, […]

如何从服务器端方法中止WCF文件上载

我正在使用WCF(.Net 4)从.Net 4 WinForms客户端应用程序上传文件到IIS服务器,用于内部系统。 我有一个MessageContract类定义如下: /// /// Message contract for uploading document data together with a file stream /// [MessageContract] public class DocumentDataFileUploadInfo : IDisposable { /// some fields omitted for brevity /// /// The stream containing the file /// [MessageBodyMember(Order = 1)] public Stream FileByteStream; /// /// Dispose of the stream if necessary /// […]

从会话中获取文件时出现“无法访问已关闭的文件”错误消息

我有一个asp.net FileUpload控件。 我可以成功上传文件到会话中的存储,但是当我想要获取它的输入流(我在HttpPosterFile中存储文件)时我收到错误 无法访问已关闭的文件 tr.PostedFile //<== HttpPostedFile; byte[] byteArray = null; using (var binaryReader = new BinaryReader(tr.PostedFile.InputStream)) { byteArray = binaryReader.ReadBytes(tr.PostedFile.ContentLength); }

是否有可unit testing的方法将文件上载到ASP.NET WebAPI?

我正在使用新的ASP.NET WebAPI的项目中工作。 我目前的任务是接受上传的文件。 到目前为止,我已经使用TDD来驱逐WebAPI代码,但我已经上传了一个墙。 我目前正在关注http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2上的建议,但似乎没有完全可以将其从unit testing中推出。 为了获取文件和表单数据,我必须使用MultipartFormDataStreamProvider ,这是不可能模拟和/或覆盖的。 如果没有放弃我的TDD方法,我该怎么办? 这是示例中的代码: public Task PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath(“~/App_Data”); var provider = new MultipartFormDataStreamProvider(root); // Read the form data and return an async task. var task = Request.Content.ReadAsMultipartAsync(provider). ContinueWith(t => { if […]