如何投放字符串HttpFilePostedBase [英] How to cast string to HttpFilePostedBase
问题描述
有没有办法如何将字符串转换HttpFilePostedBase?
我目前使用的阿贾克斯文件上传。但是,它返回的值是字符串。但是,我的方法是请求 HttpFilePostedBase
有没有办法如何铸造或将其转换为 HttpFilePostedBase
?
Is there a way how to convert the string to HttpFilePostedBase?
I'm currently using the Ajax File upload . But the value that it return is string. But my method is requesting for HttpFilePostedBase
is there a way how to cast or convert it to HttpFilePostedBase
?
下面是在文件上传我的样本方法。
here's my sample method in uploading files.
public bool uploadfiles(HttpPostedFileBase filedata)
{
bool status = false;
//code for uploading goes here
return status;
}
我如何可以调用此方法如果AJAX文件上传传递一个字符串?
How can i call this method if the ajax file upload is passing a string?
推荐答案
您使用IE或Chrome / Firefox的?原因,不同的浏览器以不同的方式上载文件。 IE浏览器上传文件,通过Requres.Files但其他人在查询字符串中使用 qqfile
。
看看这里如何使用valums与MVC为不同的浏览器
Are you using IE or Chrome/Firefox? cause, different browsers upload files in a different manner. IE uploads the files through Requres.Files but others use qqfile
in the query string.
Take a look here on how to use valums with mvc for different browsers
编辑:好吧,那么,这个怎么样。这是为我工作的一个例子:
Okay then, how about this. This is an example which worked for me:
public void ControllerUploadHandler()
{
// Set the response return data type
this.Response.ContentType = "text/html";
try
{
// get just the original filename
byte[] buffer = new byte[Request.ContentLength];
if (Request.QueryString["qqfile"] != null)
{
using (BinaryReader br = new BinaryReader(this.Request.InputStream))
br.Read(buffer, 0, buffer.Length);
}
else if (Request.Files.Count > 0)
{
HttpPostedFileBase httpPostedFileBase = Request.Files[0] as HttpPostedFileBase;
using (BinaryReader br = new BinaryReader(httpPostedFileBase.InputStream))
br.Read(buffer, 0, buffer.Length);
}
else
this.Response.Write(" {'success': false }");
// return the json object as successful
this.Response.Write("{ 'success': true }");
this.Response.End();
return;
}
catch (Exception)
{
// return the json object as unsuccessful
this.Response.Write("{ 'success': false }");
this.Response.End();
}
}
这篇关于如何投放字符串HttpFilePostedBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!