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;

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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