微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 初始化属性时出现问题

我一直收到这个错误
CS0501:’jQueryUploadTest.Upload.Filesstatus.thumbnail_url.get’必须声明一个正文,因为它没有标记为抽象或外部

我在我自己的独立网站上的sharepoint项目之外有这个确切的代码,它工作正常.每当我尝试在我的sharepoint项目中实现它时,我都会遇到错误.

这是我的upload.ashx文件代码

<%@ WebHandler Language="C#" Class="jQueryUploadTest.Upload" %>

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
using System.Web;
using System.Web.Script.Serialization;

namespace jQueryUploadTest {

    public class Upload : IHttpHandler {
        public class Filesstatus
        {
            public string thumbnail_url { get; set; }
            public string name { get; set; }
            public string url { get; set; }
            public int size { get; set; }
            public string type { get; set; }
            public string delete_url { get; set; }
            public string delete_type { get; set; }
            public string error { get; set; }
            public string progress { get; set; }
        }
        private readonly JavaScriptSerializer js = new JavaScriptSerializer();
        private string ingestPath;
        public bool IsReusable { get { return false; } }
        public void ProcessRequest (HttpContext context) {
            //var r = context.Response;
            ingestPath = @"C:\temp\ingest\";

            context.response.addheader("Pragma","no-cache");
            context.response.addheader("Cache-Control","private,no-cache");

            HandleMethod(context);
        }

        private void HandleMethod (HttpContext context) {
            switch (context.Request.HttpMethod) {
                case "HEAD":
                case "GET":
                    ServeFile(context);
                    break;

                case "POST":
                    UploadFile(context);
                    break;

                case "DELETE":
                    DeleteFile(context);
                    break;

                default:
                    context.Response.ClearHeaders();
                    context.Response.StatusCode = 405;
                    break;
            }
        }

        private void DeleteFile (HttpContext context) {
            string filePath = ingestPath + context.Request["f"];
            if (File.Exists(filePath)) {
                File.Delete(filePath);
            }
        }

        private void UploadFile (HttpContext context) {
            List<Filesstatus> statuses = new List<Filesstatus>();
            System.Collections.Specialized.NameValueCollection headers = context.Request.Headers;

            if (string.IsNullOrEmpty(headers["X-File-Name"])) {
                UploadWholeFile(context,statuses);
            } else {
                UploadPartialFile(headers["X-File-Name"],context,statuses);
            }


            WriteJsonIframeSafe(context,statuses);
        }

        private void UploadPartialFile (string fileName,HttpContext context,List<Filesstatus> statuses) {
            if (context.Request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request");
            Stream inputStream = context.Request.Files[0].InputStream;
            string fullName = ingestPath + Path.GetFileName(fileName);

            using (FileStream fs = new FileStream(fullName,FileMode.Append,FileAccess.Write)) {
                byte[] buffer = new byte[1024];

                int l = inputStream.Read(buffer,1024);
                while (l > 0) {
                    fs.Write(buffer,l);
                    l = inputStream.Read(buffer,1024);
                }
                fs.Flush();
                fs.Close();
            }
            Filesstatus MyFileStatus = new Filesstatus();
            MyFileStatus.thumbnail_url = "Thumbnail.ashx?f=" + fileName;
            MyFileStatus.url = "Upload.ashx?f=" + fileName;
            MyFileStatus.name = fileName;
            MyFileStatus.size = (int)(new FileInfo(fullName)).Length;
            MyFileStatus.type = "image/png";
            MyFileStatus.delete_url = "Upload.ashx?f=" + fileName;
            MyFileStatus.delete_type = "DELETE";
            MyFileStatus.progress = "1.0";

          /*  {
                thumbnail_url = "Thumbnail.ashx?f=" + fileName,url = "Upload.ashx?f=" + fileName,name = fileName,size = (int)(new FileInfo(fullName)).Length,type = "image/png",delete_url = "Upload.ashx?f=" + fileName,delete_type = "DELETE",progress = "1.0"
            };
            */
            statuses.Add(MyFileStatus);

        }

        private void UploadWholeFile(HttpContext context,List<Filesstatus> statuses) {
            for (int i = 0; i < context.Request.Files.Count; i++) {
                HttpPostedFile file = context.Request.Files[i];
                file.SaveAs(ingestPath + Path.GetFileName(file.FileName));
                string fileName = Path.GetFileName(file.FileName);
                Filesstatus MyFileStatus = new Filesstatus();
                MyFileStatus.thumbnail_url = "Thumbnail.ashx?f=" + fileName;
                MyFileStatus.url = "Upload.ashx?f=" + fileName;
                MyFileStatus.name = fileName;
                MyFileStatus.size = file.ContentLength;
                MyFileStatus.type = "image/png";
                MyFileStatus.delete_url = "Upload.ashx?f=" + fileName;
                MyFileStatus.delete_type = "DELETE";
                MyFileStatus.progress = "1.0";
                statuses.Add(MyFileStatus);
            }
        }

        private void WriteJsonIframeSafe(HttpContext context,List<Filesstatus> statuses) {
            context.response.addheader("vary","Accept");
            try {
                if (context.Request["HTTP_ACCEPT"].Contains("application/json")) {
                    context.Response.ContentType = "application/json";
                } else {
                    context.Response.ContentType = "text/plain";
                }
            } catch {
                context.Response.ContentType = "text/plain";
            }

            string jsonObj = js.Serialize(statuses.ToArray());
            context.Response.Write(jsonObj);
        }

        private void ServeFile (HttpContext context) {
            if (string.IsNullOrEmpty(context.Request["f"])) ListCurrentFiles(context);
            else DeliverFile(context);
        }

        private void DeliverFile (HttpContext context) {
            string filePath = ingestPath + context.Request["f"];
            if (File.Exists(filePath)) {
                context.Response.ContentType = "application/octet-stream";
                context.Response.WriteFile(filePath);
                context.response.addheader("Content-disposition","attachment,filename=\"" + context.Request["f"] + "\"");
            } else {
                context.Response.StatusCode = 404;
            }
        }

        private void ListCurrentFiles (HttpContext context) {
            List<Filesstatus> files = new List<Filesstatus>();

            string[] names = Directory.GetFiles(@"C:\temp\ingest","*",SearchOption.TopDirectoryOnly);

            foreach (string name in names) {
                FileInfo f = new FileInfo(name);
                Filesstatus MyFileStatus = new Filesstatus();
                MyFileStatus.thumbnail_url = "Thumbnail.ashx?f=" + f.Name;
                MyFileStatus.url = "Upload.ashx?f=" + f.Name;
                MyFileStatus.name = f.Name;
                MyFileStatus.size = (int)f.Length;
                MyFileStatus.type = "image/png";
                MyFileStatus.delete_url = "Upload.ashx?f=" + f.Name;
                MyFileStatus.delete_type = "DELETE";

                files.Add(MyFileStatus);
                /*files.Add(new Filesstatus
                {
                    thumbnail_url = "Thumbnail.ashx?f=" + f.Name,url = "Upload.ashx?f=" + f.Name,name = f.Name,size = (int)f.Length,delete_url = "Upload.ashx?f=" + f.Name,delete_type = "DELETE"
                });*/
            }

            context.response.addheader("Content-disposition","inline,filename=\"files.json\"");
            string jsonObj = js.Serialize(files.ToArray());
            context.Response.Write(jsonObj);
            context.Response.ContentType = "application/json";
        }
    }
}

我已经检查了我的版本定位及其asp.net 3.5,我去了我的项目及其3.5的属性所以我不认为这是问题.

解决方法

我刚刚意识到当我发现这条线时发生的事情:

<%@ WebHandler Language="C#" Class="jQueryUploadTest.Upload" %>

你正在制作一个页面句柄(可能是* .ashx),毫无疑问,它是在ASP.Net 1.1应用程序池下编译的.这不支持C#3.0构造,因此失败.

看看您是否可以将网站移动到ASP.Net 2.0应用程序池.问题应该消失.请注意,框架1.1和2.0之间存在 – 次要 – 不兼容的更改,可能需要您调整web.config和其他细节.

我不想要那个,最简单的方法是简单地不使用C#3.0样式结构:

public class Filesstatus
{

    private string m_thumbnail_url;
    private string m_name;
    private string m_url;
    private int m_size;
    private string m_type;
    private string m_delete_url;
    private string m_delete_type;
    private string m_error;
    private string m_progress;

    public string m_thumbnailurl { get { return m_thumbnail_url; } set { m_thumbnail_url = value; } }
    public string name { get { return m_name; } set { m_name = value; } }
    public string url { get { return m_url; } set { m_url = value; } }
    public int size { get { return m_size; } set { m_size = value; } }
    public string type { get { return m_type; } set { m_type = value; } }
    public string m_deleteurl { get { return m_delete_url; } set { m_delete_url = value; } }
    public string m_deletetype { get { return m_delete_type; } set { m_delete_type = value; } }
    public string error { get { return m_error; } set { m_error = value; } }
    public string progress { get { return m_progress; } set { m_progress = value; } }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐