Asp:FileUpload控件,用于多张图片上传多次返回同一张图片

时间:2019-11-05 14:07:24

标签: c# asp.net

我正在使用asp:fileupload控件一次上传多个图像。问题是,如果我上传2张图片,我会得到两次相同的文件。我从事这项工作的时间超过了我想承认的时间,因此我们将不胜感激。

Markup:
   <asp:Panel runat="server" ID="pnlUploadPhoto">
        <table>
            <tr>
                <td style="text-align: right; width: 12%;" class="controlLabel">File:</td>
                <td style="width: 2%;"></td>
                <td style="width: 86%;">
                    <asp:FileUpload runat="server" ID="fuRevisionUpload" AllowMultiple="True" style="width: 100%;" CssClass="progressMessage"/>
                    <br/>
                </td>
            </tr>
            <tr><td class="spacerRow"></td></tr>
            <tr><td class="spacerRow"></td></tr>
        </table>
    </asp:Panel>

    <table cellpadding="0" cellspacing="0">
        <tr class="spacerRow"><td></td></tr>
        <tr>
            <td style="width: 2%;"></td>
            <td style="width: 75%;" class="progressMessage">
                <asp:Label runat="server" ID="lblUploadingMessage" style="display: none;">Uploading file (may take a few moments) ...</asp:Label>
            </td>
            <td style="width: 23%;"></td>
        </tr>
    </table>

    <asp:Panel runat="server" ID="pnlImgPreviewWM" Height="350" CssClass="flexCenter flexOn">
        <table>
            <tr>
                <td class="watermarkFont">IMAGE PREVIEW</td>
            </tr>
        </table>
    </asp:Panel>
    <asp:Panel runat="server" ID="pnlImgPreviewContainer" Height="100%"  CssClass="flexCenter flexOff">
        <asp:Panel runat="server" ID="pnlImgPreview">
            <asp:Image runat="server" ID="imgPreview" CssClass="imgPreview" style="width: 100%; height: 100%;" />
        </asp:Panel>
    </asp:Panel>

下面的我的C#。这是发生在btn_Save事件上的SaveFile方法。我正在获取重复的二进制数据,因此根据执行的上传次数,只有一张图像会填充几次。

C#     SaveFile() method is where data duplication is happening


        private void SaveFile()
        {

            //for (int i = 0; i < fuRevisionUpload.PostedFiles.Count; i++)

            foreach (HttpPostedFile postedFile in fuRevisionUpload.PostedFiles)

            {

                FileInfo fi = new FileInfo(postedFile.FileName);

                bool isVirusFree = true;
                bool vScanError = false;
                string errMessage = string.Empty;
                string errStackTrace = string.Empty;

                //  Check for known file types.
                //Will need to address this as well!!
                if (FileAttachmentType.GetTypeViaExtension(fi.Extension) == FileAttachmentTypes.UnknownFileType)
                {
                    FileAttachmentError.LogError(postedFile.FileName, _user.WebUserID, "Unsupported file type.");
                    return;
                }

                //  Check for maximum file size.
                if (postedFile.ContentLength > Global.MaxFileUploadSize)
                {
                    FileAttachmentError.LogError(postedFile.FileName, _user.WebUserID,
                        string.Format("File of Unusual Size (FUS) [{0:#,##0} bytes].",
                            postedFile.ContentLength));
                    return;
                }

                //  Scan the file for viruses.
                string quarantinePath =
                    Server.MapPath(string.Format("~/Quarantine/{0}{1}", Guid.NewGuid(), fi.Extension));
                //This will need some attention!!!!!!
                //fuRevisionUpload.PostedFiles[i].SaveAs(quarantinePath);
                postedFile.SaveAs(quarantinePath);


                //  Run virus scan on the file. If any errors occur, save them, and then delete the file
                //  from the quarantine directory.
                try
                {
                    //  isVirusFree = (new ScanClient()).RunVirusScan(quarantinePath, ConfigurationManager.AppSettings["MetaScanURL"]);
                }
                catch (Exception exp)
                {
                    vScanError = true;
                    errMessage = exp.Message;
                    errStackTrace = exp.StackTrace;
                }
                finally
                {
                    //  Cleanup the test file.
                    File.Delete(quarantinePath);
                }

                //  If there were issues with the uploaded file, load the issues/errors.
                if (isVirusFree == false || vScanError == true)
                {
                    FileAttachmentError.LogError(postedFile.FileName, _user.WebUserID, errMessage, errStackTrace,
                        fuRevisionUpload.FileBytes);
                    return;
                }

                //  Now that we know that the file is safe and valid, we save the 
                //  BLOB and return to the caller.
                _attachment = new FileAttachment();

                //  Setup local variables to use for saving and/or rotating the image.
                int rotationAngle = Convert.ToInt32(hdnCurrentRotation.Value);
                int wip = Convert.ToInt32(hdnWidthInPixels.Value);
                int hip = Convert.ToInt32(hdnHeightInPixels.Value);
                //  Setup the RotateFlipType to use when rotating the image.
                RotateFlipType rotateType = RotateFlipType.RotateNoneFlipNone;
                string formattedRotateFlipTypeValue = string.Format("Rotate{0}FlipNone", rotationAngle);

                ////  Setup the memory stream and the normal bitmap.

                MemoryStream normalImage = new MemoryStream();

                Stream fs = postedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Bitmap normalBitMap = ZPLBitmap.ScaleToSize(br.ReadBytes((Int32)fs.Length), wip, hip);

                //  Validate we are getting a valid image rotation from the page.
                if (rotationAngle != 0 &&
                    RotateFlipType.TryParse(formattedRotateFlipTypeValue, out rotateType) == false) {
                    _RadAjaxMgr.Alert("Invalid image rotation.");
                    return;
                }

                ////  if we have a image that has been rotated, then write it out to the stream.
                if (rotationAngle != 0)
                    normalBitMap.RotateFlip(rotateType);

                //  Save the image.


                normalBitMap.Save(normalImage, ImageFormat.Bmp);

                _attachment.Contents = normalImage.ToArray();
                _attachment.WidthInPixels = normalBitMap.Width;
                _attachment.HeightInPixels = normalBitMap.Height;
                _attachment.OriginalFilename = postedFile.FileName;
                _attachment.VirusScanPassed = true;


                FileAttachmentTypes ext = FileAttachmentType.GetTypeViaExtension(fi.Extension);

                //  Check if the image qualifies as a large image. If we have rotated the image,
                //  no need to rotate again as we are just taking the bytes from the image that
                //  we've already rotated and scaling them to the large image size.
                if (ext != FileAttachmentTypes.PDF && ext != FileAttachmentTypes.TIF) {
                    //  if the width and height is bigger than the max values, then save 
                    //  in the large image format.
                    if (wip > MaxScaleWidth || hip > MaxScaleHeight) {

                        Bitmap scaleBMP = ZPLBitmap.ScaleToSize(fuRevisionUpload.FileBytes, MaxScaleWidth, MaxScaleHeight);

                        MemoryStream shrunkImageStream = new MemoryStream();

                        //  if we have a image that has been rotated, then write it out to the stream.
                        if (rotationAngle != 0)
                            scaleBMP.RotateFlip(rotateType);

                        //  Save the image.
                        scaleBMP.Save(shrunkImageStream, ImageFormat.Bmp);

                        _attachment.Contents = shrunkImageStream.ToArray();
                        _attachment.WidthInPixels = scaleBMP.Width;
                        _attachment.HeightInPixels = scaleBMP.Height;

                        _attachment.LargeImage = normalImage.ToArray();
                        _attachment.LargeImageWidthInPixels = normalBitMap.Width;
                        _attachment.LargeImageHeightInPixels = normalBitMap.Height;

                        hasLargeImage = true;
                    }
                }


                _attachment.Save(FileAttachmentType.GetTypeIDViaExtension(fi.Extension), _user.WebUserID);
                _sbfileAttachmentIDs.AppendFormat("{0},", _attachment.FileAttachmentID);

            }
        }

0 个答案:

没有答案