0

我正在使用以下代码将图像文件上传到 SharePoint 文档库。该代码在本地运行良好,但一旦部署到服务器,我就会收到异常,因为找不到文件。

                String fileToUpload = FlUpldImage.PostedFile.FileName; //@"C:\Users\admin.RSS\Desktop\Photos\me_skype.jpg";
                String documentLibraryName = "SiteAssets";
                if (!System.IO.File.Exists(fileToUpload))
                    throw new FileNotFoundException("File not found.", fileToUpload);

                SPFolder myLibrary = web.Folders[documentLibraryName];

                // Prepare to upload
                Boolean replaceExistingFiles = true;
                String fileName = CheckStringNull(txtFirstName.Text) + CheckStringNull(txtLastName.Text) + CheckDateNull(txtDOB) + System.IO.Path.GetFileName(fileToUpload); ;
                if (fileName.Contains('/'))
                {
                    fileName = fileName.Replace("/", "");
                }
                if (fileName.Contains(':'))
                {
                    fileName = fileName.Replace(":", "");
                }
                FileStream fileStream = File.OpenRead(fileToUpload);
                //Upload document
                SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
                string url = site.ToString() + "/" + spfile.ToString();
                if (url.Contains("="))
                {
                    url = url.Split('=')[1];
                }
                //Commit
                myLibrary.Update();

字符串 fileupload 包含 URL,因为C:\Users\admin.RSS\Desktop\Photos\me.jpg这个 URL 实际上是客户端系统,服务器端代码抛出异常,因为找不到文件。如何处理这个问题?

更新:

我删除了检查文件是否存在的代码行,现在我得到了FileStream fileStream = File.OpenRead(fileToUpload);例外c:\windows\system32\inetsrv\20120605_133145.jpg cold not be found

请帮忙。谢谢你

4

2 回答 2

0
        if (this.fuAvatarUpload.HasFile && this.fuAvatarUpload.PostedFile.FileName.Length > 0)
        {
            string extension = Path.GetExtension(file.FileName).ToLower();
            string mimetype;
            switch (extension)
            {
                case ".png":
                case ".jpg":
                case ".gif":
                    mimetype = file.ContentType;
                    break;

                default:
                    _model.ShowMessage("We only accept .png, .jpg, and .gif!");
                    return;
            }

            if (file.ContentLength / 1000 < 1000)
            {
                Image image = Image.FromStream(file.InputStream);
                Bitmap resized = new Bitmap(image, 150, 150);
                byte[] byteArr = new byte[file.InputStream.Length];
                using (MemoryStream stream = new MemoryStream())
                {
                    resized.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byteArr = stream.ToArray();
                }
                file.InputStream.Read(byteArr, 0, byteArr.Length);
                profile.ImageUrl = byteArr;
                profile.UseGravatar = false;
                profileService.UpdateProfile(profile);
                this._model.ShowApprovePanel();
            }
            else
            {
                _model.ShowMessage("The file you uploaded is larger than the 1mb limit.  Please reduce the size of your file and try again.");
            }
        }
于 2012-08-09T12:06:09.400 回答
0

将文件物理地保存到服务器上,而不是在服务器上工作,这帮助我解决了我的问题。

于 2012-08-17T12:50:48.503 回答