سؤال

لدي طريقة تمديد أدناه، ولكن عندما أقوم بتشغيل هذا، فإنه يعطيني InvalidCastException ويقول *

غير قادر على إلقاء كائن من النوع "system.string" لكتابة "system.web.httppostedfile".

رمز :

public static List<Attachment> GetFiles(this HttpFileCollection collection) {
            if (collection.Count > 0) {
                List<Attachment> items = new List<Attachment>();
                foreach (HttpPostedFile _file in collection) {
                    if (_file.ContentLength > 0)
                        items.Add(new Attachment()
                        {
                            ContentType = _file.ContentType,
                            Name = _file.FileName.LastIndexOf('\\') > 0 ? _file.FileName.Substring(_file.FileName.LastIndexOf('\\') + 1) : _file.FileName,
                            Size = _file.ContentLength / 1024,
                            FileContent = new Binary(new BinaryReader(_file.InputStream).ReadBytes((int)_file.InputStream.Length))
                        });

                    else
                        continue;
                }
                return items;
            } else
                return null;
        }

شكرا مقدما.

يقول MSDN:

يقوم العملاء بتشفير الملفات ونقلها في هيكل المحتوى باستخدام تنسيق MIME متعدد الأقطار مع وجود رأس نوع محتوى HTTP من بيانات متعددة الأجزاء / النموذج. يستخرج ASP.NET الملف (الملفات) المشفرة من هيكل المحتوى إلى أعضاء فرديين في HttpFilecollection. توفر طرق وخصائص فئة HTTPPostEdFile الوصول إلى محتويات وخصائص كل ملف.

هل كانت مفيدة؟

المحلول

إذا نظرت إلى نموذج التعليمات البرمجية في هذه الصفحة، فهذا يوضح كيفية تعداد المجموعة، فأنت في الواقع الحصول على سلسلة عند محاولة التعداد كما أنت.

http://msdn.microsoft.com/en-us/library/system.web.httpfilecollection.aspx.

نصائح أخرى

ال HttpFileCollection يعيد مجموعة العدادات المفاتيح. تحتاج إلى استخدام المفتاح في كل تكرار للحلقة للبحث عن المرتبط HttpPostedFile هدف. لذلك الحلقة تحتاج إلى أن تبدو مثل هذا:

foreach (string name in collection) {
    HttpPostedFile _file = collection[name];
    // ...rest of your loop code...
}

حسنا لقد وجدت حلا ولكن يبدو غبي جدا لكنه يعمل.

لقد غيرت ببساطة foreach مع هذا الشخص :

foreach (string fileString in collection.AllKeys) {
                    HttpPostedFile _file = collection[fileString];
                    if (_file.ContentLength > 0)

                        items.Add(new Attachment()
                        {
                            ContentType = _file.ContentType,
                            Name = _file.FileName.LastIndexOf('\\') > 0 ? _file.FileName.Substring(_file.FileName.LastIndexOf('\\') + 1) : _file.FileName,
                            Size = _file.ContentLength / 1024,
                            FileContent = new Binary(new BinaryReader(_file.InputStream).ReadBytes((int)_file.InputStream.Length))
                        });

                    else
                        continue;
                }
HttpFileCollection hfc = Request.Files;
  for (int i = 0; i < hfc.Count; i++)
  {
     HttpPostedFile hpf = hfc[i];
     if (hpf.ContentLength > 0)
    {
     string _fileSavePath = _DocPhysicalPath  + "_" + hpf.FileName;
    }
  }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top