Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

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

1 Answer 1

up vote 0 down vote accepted

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);
share|improve this answer
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 May 22 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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