如何将HttpPostedFileBase对象序列化为json

时间:2022-10-29 19:44:11

I have a a method in controller named 'Upload' with a parameter HttpPostedFileBase object, I posted the file from view and saved it to folder successfully. But When I try to return an Json string object with below contents it throws exception with messaje:"Error getting value from 'ReadTimeout' on 'System.Web.HttpInputStream'."

我在控制器中有一个名为'Upload'的方法,带有参数HttpPostedFileBase对象,我从视图中发布了该文件并成功保存到文件夹中。但是当我尝试返回一个带有以下内容的Json字符串对象时,它会抛出异常与messaje:“在'System.Web.HttpInputStream''从'ReadTimeout'获取值时出错。”

And it case of 'files = files,' line if I delete it, it returns correct. But I need this data

并且它的'files = files,'行如果我删除它,它返回正确。但我需要这些数据

public string Upload(HttpPostedFileBase files)
            {
                try
                {

                    if (files != null && files.ContentLength > 0)
                    {
                        var path = Path.Combine(Server.MapPath("~/Uploads"), files.FileName);
                        files.SaveAs(path);
                        return JsonConvert.SerializeObject(
                            new
                        {
                            files=files,
                            Passed = true,
                            Mesaj = "item added"
                        });
                    }

1 个解决方案

#1


4  

You can create custom JsonConverter like this:

您可以像这样创建自定义JsonConverter:

public class HttpPostedFileConverter : JsonConverter
{
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var stream = (Stream)value;
            using (var sr = new BinaryReader(stream))
            {
                var buffer = sr.ReadBytes((int)stream.Length);
                writer.WriteValue(Convert.ToBase64String(buffer));
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType.IsSubclassOf(typeof(Stream));
        }
    }

And pass it to JsonConvert.SerializeObject method

并将其传递给JsonConvert.SerializeObject方法

return JsonConvert.SerializeObject(
                    new
                    {
                        files=files,
                        Passed = true,
                        Mesaj = "item added"
                    }, 
                    new HttpPostedFileConverter());

#1


4  

You can create custom JsonConverter like this:

您可以像这样创建自定义JsonConverter:

public class HttpPostedFileConverter : JsonConverter
{
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var stream = (Stream)value;
            using (var sr = new BinaryReader(stream))
            {
                var buffer = sr.ReadBytes((int)stream.Length);
                writer.WriteValue(Convert.ToBase64String(buffer));
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType.IsSubclassOf(typeof(Stream));
        }
    }

And pass it to JsonConvert.SerializeObject method

并将其传递给JsonConvert.SerializeObject方法

return JsonConvert.SerializeObject(
                    new
                    {
                        files=files,
                        Passed = true,
                        Mesaj = "item added"
                    }, 
                    new HttpPostedFileConverter());