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 created a web application for uploading files and associated metadata. When I test the solution on my laptop, it works without a problem (file gets uploaded and metadata is saved). When I try the deployed solution on the server, I get file not found error: 'Could not find file 'c:\windows\system32\inetsrv\award.txt'. Through my application, I'm trying to upload a file from my laptop, so I'm not sure what is going on. Can someone tell me how to fix this?

            if (fileUpload.HasFile)
            {

                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        using (FileStream fs = (new FileInfo(fileUpload.PostedFile.FileName)).OpenRead())
                        {
                            SPList list = web.Lists["Awards"];

                            //Add the initial metadata. We will do an update for all lookup table values, as they need to be done after the record is created.
                            Hashtable ht = new Hashtable();
                            ht.Add("wfRecipientRank", ddRanks.SelectedValue);
                            ht.Add("wfRecipientName", txtRecipientName.Text);
                            ht.Add("APFT", rbPassedApft.SelectedValue);
                            ht.Add("HeightWeight", rbMeetHtWt.SelectedValue);
                            ht.Add("Posthumous", rbPosthumous.SelectedValue);

                            SPFile file = list.RootFolder.Files.Add(fileUpload.FileName, fs, ht, false);
                            SPListItem item = file.Item;

                            //Get the lookup field values
                            SPList lookupList = web.Lists["AwardType"];
                            int awardTypeId = GetItemId(ddAwardTypes.SelectedValue, lookupList);
                            SPFieldLookupValue awardTypeLookupValue = new SPFieldLookupValue();

                            if (awardTypeId > 0)
                                awardTypeLookupValue = new SPFieldLookupValue(awardTypeId, ddAwardTypes.SelectedValue);

                            lookupList = web.Lists["AwardReason"];
                            int awardReasonId = GetItemId(ddAwardReasons.SelectedValue, lookupList);
                            SPFieldLookupValue awardReasonLookupValue = new SPFieldLookupValue();

                            if (awardReasonId > 0)
                                awardReasonLookupValue = new SPFieldLookupValue(awardReasonId, ddAwardReasons.SelectedValue);

                            lookupList = web.Lists["Organization"];

                            MilitaryUnit organizationSelected = null;
                            SPFieldLookupValue organizationLookupValue = new SPFieldLookupValue();

                            if (Session["OrganizationSelected"] != null)
                            {
                                organizationSelected = (MilitaryUnit)Session["OrganizationSelected"];
                                int organizationId = GetItemId(organizationSelected.uic, lookupList);

                                if (organizationId > 0)
                                    organizationLookupValue = new SPFieldLookupValue(organizationId, txtOrganization.Value);
                            }

                            item["Organization"] = organizationLookupValue;
                            item["AwardType"] = awardTypeLookupValue;
                            item["AwardReason"] = awardReasonLookupValue;
                            item.Update();
                        }
                    }
                }
            }
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

The problem is you are opening a stream server side. This just won't work because the file does not exist server side. The FileUpload control has a FileBytes property that you can use.

//Off the top of my head I can't remember if ServerRelativeUrl contains a trailing slash so you might need to add one.
string fileUrl = string.Format(list.RootFolder.ServerRelativeUrl{0}, fileUpload.FileName); 

SPFile file = list.RootFolder.Files.Add(fileUrl, fileUpload.FileBytes);

I know this is code example short and sweet but I think it will very easily get the point across.

This is how I usually do it, but if you really want to use a stream there is FileUpload.PostedFile.InputStream.

share|improve this answer
add comment

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.