我幾次使用這個jQuery pluging。
我把我的上傳按鈕放入jQuery UI modal dialog,它使用PopupImageUploader元素。
<div id="PopupImageUploader" title="Upload Image">
<div id="uploaderFile"></div>
</div>
和我的JavaScript構建的元素上傳upladerFile
function CreateImageUploader() {
var uploader = new qq.FileUploader({
element: $('#uploaderFile')[0],
template: '<div class="qq-uploader">' +
'<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' +
'<div class="qq-upload-button ui-button ui-widget ui-corner-all ui-button-text-only ui-state-default">Seleziona il Listino Excel</div>' +
'<ul class="qq-upload-list"></ul>' +
'</div>',
hoverClass: 'ui-state-hover',
focusClass: 'ui-state-focus',
action: 'Home/UploadImage',
allowedExtensions: ['jpg', 'gif'],
params: { },
onSubmit: function(file, ext) {
},
onComplete: function(id, fileName, responseJSON) {
$("#PopupImageUploader").dialog('close');
}
}
});
}
可以使用的onComplete事件查看上傳的結果,最終,更新下拉。 您的上傳圖像動作可以接收params: { }
屬性中指定的額外參數。 這是我的控制器:
[HttpPost()]
public System.String UploadImage(string id)
{
bool IsIE = false;
string sFileName = "";
var TempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_TEMP");
if ((Request.Files == null) || (Request.Files.Count == 0))
{
if (string.IsNullOrEmpty(Request.Params["qqfile"]))
{
return ("{success:false, error:'request file is empty'}");
}
else
{
sFileName = Request.Params["qqfile"].ToString();
}
}
else
{
sFileName = Request.Files[0].FileName;
IsIE = true;
}
if (string.IsNullOrEmpty(sFileName))
{
return ("{success:false, error:'request file is empty'}");
}
string DocumentName = id + Path.GetExtension(sFileName);
if (IsIE)
{
try
{
Request.Files[0].SaveAs(Path.Combine(TempFolder, DocumentName));
}
catch (Exception ex)
{
return ("{success:false, error:'" + ex.Message + "'}");
}
}
else
{
try
{
if ((Request.InputStream != null) && (Request.InputStream.CanRead) && (Request.InputStream.Length > 0))
{
using (FileStream fileStream = new FileStream(Path.Combine(TempFolder, DocumentName), FileMode.Create))
{
byte[] FileBytes = new byte[Convert.ToInt32(Request.InputStream.Length) + 1];
Int32 bytesRead = 0;
bytesRead = Request.InputStream.Read(FileBytes, 0, FileBytes.Length);
fileStream.Write(FileBytes, 0, bytesRead);
fileStream.Flush();
fileStream.Close();
}
}
}
catch (Exception ex)
{
return ("{success:false, error:'" + ex.Message + "'}");
}
}
var newFileName = "new assigned filename";
return ("{success:true, newfilename: '" + newFileName + "'}");
}
IE瀏覽器有不同的行爲,所以我有兩個不同的程序來讀取文件。
您可以使用formdata在httppostfilebase中發送圖像。 – user3789888 2014-07-28 05:56:08