1

I'm using the code below to allow users to upload files as attachments to items in a SharePoint (2010) List.

How can I add type and size restrictions to the attachment, for example, if I wanted the attachments to succeed only if the file was a .JPG under 5MB in size?

<input type="file" runat="server" id="fuUploadFile" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> 

Code behind:

Code behind

protected void Page_Load(object sender, EventArgs e) { }

protected void btnUpload_Click(object sender, EventArgs e)
{
    SPWeb web = SPContext.Current.Web;
    SPList lst = web.Lists["SampleList"];
    SPListItem item = lst.Items.GetItemById(1);
    SPAttachmentCollection attachments = item.Attachments;
    attachments.Add(fuUploadFile.PostedFile.FileName, ReadStream(fuUploadFile.PostedFile.InputStream));
    item.Update();
}
public static byte[] ReadStream(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
1

The following example code for your reference.

<p>
    <asp:Label ID="userMessage" runat="server" Text=""></asp:Label>
</p>
<p>
    <asp:FileUpload ID="fuUploadFile" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</p>

Code behind:

protected void btnUpload_Click(object sender, EventArgs e)
{
    if (!fuUploadFile.HasFile)
    {
        userMessage.Text = "Please select a file before clicking upload.";
        return;
    }
    //Check the file is less than 5MB
    int fileSize = fuUploadFile.PostedFile.ContentLength;
    if (fileSize > 5000000)
    {
        userMessage.Text += String.Format("File Size Exceeds 5MB. Choose a smaller file.");
        return;
    }

    String imageFileExtension = Path.GetExtension(fuUploadFile.FileName);
    //Check the user has selected a jpg image
    if (imageFileExtension == null || imageFileExtension.ToLower() != ".jpg")
    {
        userMessage.Text += "The file you have selected is not in the right format. Please use a jpg image.";
        return;
    }
    userMessage.Text = "";
    SPWeb web = SPContext.Current.Web;
    SPList lst = web.Lists["SampleList"];
    SPListItem item = lst.Items.GetItemById(1);
    SPAttachmentCollection attachments = item.Attachments;
    attachments.Add(fuUploadFile.PostedFile.FileName, ReadStream(fuUploadFile.PostedFile.InputStream));
    item.Update();
}
public static byte[] ReadStream(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

Refer to: SharePoint: Build a Webpart to Resize and Upload Images to a Picture Library

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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