更新
我能够让一切正常工作,我只想发回更新的代码。我使用了 Darin Dimitrov 关于使用单独的通用 http 处理程序来处理文件上传的建议,所以这是我为此提出的代码……如果您有任何问题,请告诉我。
<%@ WebHandler Language="C#" Class="Upload" %>
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Web;
public class Upload : IHttpHandler {
public void ProcessRequest(HttpContext context) {
/**
* If 'newTicket' is "false", then the directory to upload to already exists and we can extract it from
* the 'ticketID' POST parameter.
*
* If 'newTicket' is "true", then this is a new Ticket submission so we need to work with a NEW directory
* on the server, so the ID needs to be 1 more than the total number of directories in ~/TicketUploads/
*/
String newTicket = context.Request["newTicket"] != null ? context.Request["newTicket"] : String.Empty;
int theID = -1;
if (newTicket.Equals("true")) {
// we need to calculate a new ID
theID = getNewID(context); // calculate the new ID = # of rows
theID++; // add 1 to make it unique
} else if (newTicket.Equals("false")) {
// we can just get the ID from the POST parameter
theID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1;
} else {
// something went wrong with the 'newTicket' POST parameter
context.Response.ContentType = "text/plain";
context.Response.Write("Error with 'newTicket' POST parameter.");
}
// if theID is negative, something went wrong... can't continue
if (theID < 0) {
return;
}
// ready to read the files being uploaded and upload them to the correct directory
int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
string fileName = context.Request["name"] != null ? context.Request["name"] : string.Empty;
var uploadPath = context.Server.MapPath("~/TicketUploads/" + theID + "/");
HttpPostedFile fileUpload = context.Request.Files[0];
// if the NEW directory doesn't exist, create it
DirectoryInfo di = new DirectoryInfo("" + uploadPath + "");
if (!(di.Exists)) {
di.Create();
}
using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append)) {
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
context.Response.ContentType = "text/plain";
context.Response.Write("File uploaded.");
return;
}
}
我正在尝试使用 C#将Plupload文件上传器集成到 ASP.NET 中。我已经阅读了Angry Monkeys 的文章以及Marco Valsecchi 的博客文章,但我有点迷茫。
上述文章建议的C#大致类似于以下内容:
int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
string fileName = Request.QueryString["name"] != null ? Request.QueryString["name"] : string.Empty;
HttpPostedFile fileUpload = Request.Files[0];
using (FileStream fs = new FileStream(Server.MapPath("~/TicketUploads/" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
{
Byte[] buffer = new Byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
首先,我设置了 Plupload 配置如下:
$("#plupload_container").pluploadQueue({
runtimes: 'html5,gears,flash,silverlight,html4',
flash_swf_url: '../plupload/js/plupload.flash.swf',
silverlight_xap_url: '../plupload/js/plupload.silverlight.xap',
filters: [
{ title: "Image files", extensions: "jpg,gif" },
{ title: "Zip files", extensions: "zip" },
{ title: "Document files", extensions: "doc,pdf,txt" }
]
});
...但我觉得我在这里遗漏了一些上传工作所必需的东西。
我想我的主要问题是如何调用上面的 C# 代码以便可以开始上传?我在名为 的页面上有一个表单SubmitRequest.aspx
。单击表单上的“提交”会产生以下结果:
$('form').submit(function (e) {
// Validate number of uploaded files
if (uploader.total.uploaded == 0) {
// Files in queue upload them first
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('UploadProgress', function () {
if (uploader.total.uploaded == uploader.files.length)
$('form').submit();
});
uploader.start();
}
e.preventDefault();
}
});
...因此,当单击“提交”并上传文件时,上传程序就会启动。完成后,将提交表单的其余部分。我不明白如何将此事件链接到将处理上传到TicketUploads
服务器上文件夹的 C# 代码。
我为这个冗长的帖子道歉,但我会很感激任何帮助:)