问题描述
我想上传一张图片,而无需使用任何asp的控件。到目前为止,我一直在想:
I'm trying to upload an image without using any of the asp controls. So far I've been trying:
<form id="form1" data-ajax="false" method="post" runat="server" enctype="multipart/form-data">
<input type="file" id="uploadFile" name="uploadFile" runat="server"/>
<input type="submit" class="btn-login" data-corners="true" value="Yükle" onclick="UploadPhoto()" />
</form>
在我的.aspx页面,并在UploadPhoto()的JQuery的方法:
in my .aspx page and in UploadPhoto() method in JQuery:
function UploadPhoto() {
$.ajax({
type: "POST",
url: "IncidentChange.aspx/UploadPhoto",
contentType: "application/json; charset=utf-8",
success: function (msg) {
},
error: function () {
}
});
}
我张贴到C#code后面。但是,我无法从codebehind达到上传的项目。
I am posting to C# code behind. However, I could not reach the uploaded item from codebehind.
[WebMethod]
public static string UploadPhoto()
{
try
{
byte[] fileData = null;
using (var binaryReader = new BinaryReader(HttpContext.Current.Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(HttpContext.Current.Request.Files[0].ContentLength);
}
}
catch (Exception ex)
{
}
return null;
}
然而Request.Files似乎是一个空数组。由于该方法是静态的([WebMethod的])我不能在法内到达输入控制,以获得发布的文件。我怎样才能解决这个问题?谢谢你。
However Request.Files appears to be a null array. Since the method is static ([WebMethod]) I cannot reach the input control within the method to get the posted file. How can I overcome this issue? Thank you.
推荐答案
HTML:
<input type="file" id="uploadFile" name="uploadFile"/>
<input type="button" id="submit" value="Submit" onClick="UploadFile();"/>
JQuery的:
<script type="text/javascript">
$(document).ready(function () {
function UploadFile() {
var fileName = $('#uploadFile').val().replace(/.*(\/|\\)/, '');
if (fileName != "") {
$.ajaxFileUpload({ url: 'AjaxFileUploader.ashx',
secureuri: false,
fileElementId: 'uploadFile',
dataType: 'json',
success: function (data, status) {
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert('Success');
}
}
},
error: function (data, status, e) {
alert(e);
}
}
)
}
}
});
请从tyhe bekow链接下载AJAX文件上传插件。
Please download ajax file uploader plugin from tyhe bekow link.
http://www.phpletter.com/DOWNLOAD/
处理程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
namespace MyProject
{
public class AjaxFileUploader : IHttpHandler
{
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
string path = context.Server.MapPath("~/UploadImages");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var file = context.Request.Files[0];
string fileName;
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
{
string[] files = file.FileName.Split(new char[] { '\\' });
fileName = files[files.Length - 1];
}
else
{
fileName = file.FileName;
}
string newFilename = Guid.NewGuid().ToString();
FileInfo fInfo = new FileInfo(fileName);
newFilename = string.Format("{0}{1}", newFilename, fInfo.Extension);
string strFileName = newFilename;
fileName = Path.Combine(path, newFilename);
file.SaveAs(fileName);
string msg = "{";
msg += string.Format("error:'{0}',\n", string.Empty);
msg += string.Format("msg:'{0}'\n", strFileName);
msg += "}";
context.Response.Write(msg);
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
这篇关于文件上传不使用任何ASP控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!