如何将字符串转换为HttpFilePostedBase

时间:2012-08-06 08:11:32

标签: asp.net asp.net-mvc-3 ajax-upload

有没有办法将字符串转换为HttpFilePostedBase? 我目前正在使用Ajax File upload。但它返回的值是字符串。但我的方法是请求HttpFilePostedBase有没有办法将其转换或转换为HttpFilePostedBase

这是我上传文件的示例方法。

public bool uploadfiles(HttpPostedFileBase filedata)
{
bool status = false;
//code for uploading goes here
return status;
}

如果ajax文件上传传递字符串,我如何调用此方法?

2 个答案:

答案 0 :(得分:0)

你做不到。您可以通过HttpContext.Request.Files属性访问发布到aspx页面的文件。

答案 1 :(得分:0)

您使用的是IE还是Chrome / Firefox?因为,不同的浏览器以不同的方式上传文件。 IE通过Requres.Files上传文件,但其他人在查询字符串中使用qqfile。 看看here关于如何将valum与mvc用于不同的浏览器

编辑:好的,那怎么样。这是一个对我有用的例子:

        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();
        }
    }