2

I'm facing a very bizzare error in SharePoint. Basically, when I upload a file to SharePoint using a custom form and a normal attachments field control:

<SharePoint:AttachmentsField runat="server" ID="fieldAttach" ControlMode="Display" FieldName="Attachments"></SharePoint:AttachmentsField>

After uploading the file, SharePoint is removing the last byte! Does anyone have any idea what may be causing the issue? We do not even have an event reciever or any other magic on this form.

This is causing the file to become corrupt. It's happening with all XPS documents (haven't check if it happens with other files yet). enter image description here

Edit: It's happening with other files too.

I'm going to post the codebehind but I don't think it will be of much help. I'll try to isolate the problem sometime tomorrow.

 protected void btnSubmit_Click(object sender, EventArgs e)
        {
                if (Page.IsValid && SPUtility.ValidateFormDigest())
                {
                    try
                    {
                        if (!ddEstimatedPaymentDate.IsDateEmpty)
                        {
                            PurchaseRequisition.EstimatedPaymentDate = ddEstimatedPaymentDate.SelectedDate;
                        }
                        else
                        {
                            PurchaseRequisition.EstimatedPaymentDate = null;
                        }

                        if (!ddDeliveryRequired.IsDateEmpty)
                        {
                            PurchaseRequisition.DeliveryRequiredBy = ddDeliveryRequired.SelectedDate;
                        }
                        else
                        {
                            PurchaseRequisition.DeliveryRequiredBy = null;
                        }

                        PurchaseRequisition.TotalAmount = Convert.ToDouble(txtTotalAmount.Text);

                        if (!string.IsNullOrEmpty(txtDepositRequired.Text))
                        {
                            PurchaseRequisition.DepositRequired = Convert.ToDouble(txtDepositRequired.Text);
                        }
                        else
                        {
#warning Since Deposit required has been identified to be nullable, nullable property may be implemented in the future
                            PurchaseRequisition.DepositRequired = 0;
                        }

                        PurchaseRequisition.PurposeOfPurchase = txtPurposeOfPurchase.Text;
                        PurchaseRequisition.NewAttachments = Page.Request.Files;


                        //extra validation before sending the purchase requisition for save.
                        if (PurchaseRequisition.ValidatePurchaseRequisition())
                        {
                            HideServerSideErrorMessages(ref txtServerSideErrors);
                            PurchaseRequisition.Update(false);
                            bool redirection = SPUtility.Redirect(CurrentList.RootFolder.ServerRelativeUrl, SPRedirectFlags.Default | SPRedirectFlags.DoNotEndResponse, HttpContext.Current);
                        }
                        else
                        {
                            DisplayServerSideErrorMessages(PurchaseRequisition, ref txtServerSideErrors);
                        }
                    }
                    catch (Exception exception)
                    {
                        ...
                    }
                }
        }

HTML:

Attachments

enter image description here

4
  • Please check whether it is happening with other files as well.
    – Unnie
    Commented May 22, 2014 at 12:50
  • Can you please post your code-behind?
    – Rob D'Oria
    Commented May 22, 2014 at 13:14
  • @Unnie By the way, this is reproducible on two servers with different environments!
    – David
    Commented May 22, 2014 at 14:27
  • 2
    Where is the code that's actually handling the attachments?
    – Rob D'Oria
    Commented May 22, 2014 at 14:33

1 Answer 1

0

Rob's question solved my issue. It's my fault because I didn't dig deep enough.

Here's the offending code:

//Get the list of files for attachments
HttpPostedFile newAttach = fileCollection[i];
byte[] fileContents = new byte[newAttach.ContentLength - 1];
newAttach.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
newAttach.InputStream.Read(fileContents, 0, newAttach.ContentLength - 1);
1
  • 1
    that's where I was going...I've seen so many people do the -1 on the byte array and Read. That's why I wanted to see the code.
    – Rob D'Oria
    Commented May 22, 2014 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.